python-engineio

repository·main·Indexed 42 days ago

https://github.com/miguelgrinberg/python-engineio

A Python implementation of the Engine.IO protocol, providing client and server capabilities for real-time, bidirectional, and event-based communication. It supports both synchronous (WSGI) and asynchronous (ASGI/asyncio) environments via classes such as engineio.Server, engineio.AsyncServer, engineio.Client, and engineio.AsyncClient. Version 4.14.1.dev0.

Tokens
16.4K
Snippets
69
Records
90
Agent score
50%

What's inside python-engineio

  1. Overview of Engine.IO Tornado examples

    main

    The examples/server/tornado/ directory contains two primary demonstration applications designed to work with the Tornado framework:

    • simple.py: A basic client-server application where the client sends messages to the server and the server sends responses back.
    • latency.py: A port of the official Engine.IO Javascript server's latency application. The client sends ping messages, the server responds with pong, and the client measures and plots the round-trip time in real time. This is useful for benchmarking the performance of different asynchronous modes supported by the Engine.IO server.
  2. Overview of Engine.IO ASGI example applications

    main

    The examples/server/asgi directory contains Engine.IO applications designed to be compatible with asyncio and the ASGI specification. There are two primary examples provided:

    1. simple.py: A basic client-server application where the client sends messages to the server and the server responds.
    2. latency.py: A port of the official Engine.IO Javascript server's latency application. The client sends ping messages, the server responds with pong, and the client measures and plots the exchange time in real time. This is useful for benchmarking the performance of different asynchronous modes supported by the Engine.IO server.
  3. Understand the Asyncio client examples

    main

    The examples/client/asyncio directory provides two distinct patterns for using Engine.IO with asyncio:

    • simple_client.py: A basic implementation where the client sends messages to the server and handles responses. This is useful for understanding standard request/response or message-passing patterns.
    • latency_client.py: A performance-focused implementation. The client sends ping messages, the server responds with pong, and the client measures the round-trip time. This is used to benchmark the performance of different asynchronous modes supported by the Engine.IO server.
  4. Understand the Engine.IO aiohttp example applications

    main

    The examples/server/aiohttp/ directory contains two primary demonstration applications designed for the asyncio and aiohttp frameworks:

    1. simple.py: A basic bidirectional communication example where a client sends messages to the server and the server responds.
    2. latency.py: A port of the official Engine.IO Javascript server's latency application. The client sends ping messages, the server responds with pong, and the client measures the round-trip time. This application is used to measure the performance of different asynchronous modes supported by the Engine.IO server and plots the results in real time on a web page.
  5. Explore Engine.IO Server implementation examples

    main

    The examples/server directory provides reference implementations of Engine.IO servers across various Python web frameworks and protocols. Use these examples to understand how to integrate Engine.IO into your specific environment.

    Available implementation types:

    • WSGI: For synchronous web servers following the WSGI specification.
    • ASGI: For asynchronous web servers following the ASGI specification.
    • aiohttp: Specifically for the aiohttp framework using asyncio.
    • Sanic: Specifically for the sanic framework using asyncio.
    • Tornado: For the Tornado web framework.
    • JavaScript: Client-side examples using the JavaScript version of Engine.IO, useful for compatibility testing between Python servers and JS clients.
  6. Understand the Engine.IO Sanic example applications

    main

    The Sanic examples demonstrate two different usage patterns for Engine.IO:

    • simple.py: A basic application demonstrating a request-response pattern where a client sends messages to the server and the server responds.
    • latency.py: A port of the official Engine.IO Javascript server's latency application. It uses a ping/pong mechanism where the client sends ping messages and the server responds with pong. The client measures the round-trip time for each exchange and plots the results in real time. This is useful for benchmarking the performance of different asynchronous modes supported by the Engine.IO server.
  7. What is Engine.IO?

    main

    Engine.IO is a lightweight transport protocol designed for real-time, bidirectional, event-based communication between clients (like web browsers) and servers.

    Key characteristics:

    • Simple Protocol: Once a connection is established, either side can send messages.
    • Event-Driven: Applications use event handlers to respond to messages, connection establishment, or connection drops.
    • Python Support: This package provides Python implementations for both client and server, including standard and asyncio variants.
    • Transports: Implements both HTTP long-polling and WebSocket transports.
  8. How the Engine.IO Client works: Standard vs asyncio

    main

    The package provides two client classes that share the same method names but differ in their execution model:

    1. engineio.Client: A client compatible with the standard Python library (synchronous).
    2. engineio.AsyncClient: A client compatible with the asyncio package. Most methods in this client are implemented as coroutines and must be awaited.

    Both clients handle background tasks (connection maintenance and event handling) automatically, allowing your main thread to perform other work.

    import engineio
    
    # standard Python
    eio = engineio.Client()
    
    # asyncio
    eio = engineio.AsyncClient()
  9. Scaling Engine.IO horizontally

    main

    Engine.IO is a stateful protocol, which requires specific configurations for horizontal scaling across multiple processes or servers:

    1. Concurrency: Each Engine.IO server process must be able to handle multiple requests concurrently. This is necessary because long-polling clients send two requests in parallel.
    2. Sticky Sessions: The load balancer must be configured with sticky sessions (or session affinity) to ensure requests from a single client are always forwarded to the same server process.
  10. Run Engine.IO aiohttp examples

    main

    To run the provided Engine.IO examples (such as simple.py or latency.py), you must use Python 3.5 or later. The process involves setting up a virtual environment, installing the necessary dependencies, and executing the script directly via the Python interpreter.

    Once running, the applications are accessible via a web browser at http://localhost:8080.

    # Create a virtual environment and install requirements first
    $ python simple.py
    
    # Or run the latency example
    $ python latency.py
  11. Explore Engine.IO examples

    main

    The examples/ directory contains reference implementations for both Engine.IO servers and clients. Use these examples to understand how to structure your applications and implement specific communication patterns.

    • Servers: Look in examples/server/ for implementations of Engine.IO server-side logic.
    • Clients: Look in examples/client/ for implementations of Engine.IO client-side logic.