python-socketio

repository·main·Indexed 26 days ago

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

A Python implementation of the Socket.IO realtime client and server enabling bidirectional, event-based communication. It provides synchronous and asynchronous interfaces (AsyncClient, AsyncServer) and supports various distributed message broadcasting managers including Redis, Kombu, Kafka, ZeroMQ, and RabbitMQ.

Tokens
21.6K
Snippets
49
Records
153
Agent score
87%

What's inside python-socketio

  1. Overview of Socket.IO Tornado example applications

    main

    The examples/server/tornado/ directory contains several demonstration applications compatible with the Tornado framework:

    • app.py: A comprehensive "kitchen sink" application designed for experimenting with most available Socket.IO server features.
    • latency.py: A port of the official Engine.IO Javascript latency application. It measures round-trip time by exchanging ping and pong messages between the client and server, plotting the results in real time. It is useful for benchmarking different asynchronous modes.
    • fiddle.py: A simple application based on a standard JavaScript Socket.IO example.
  2. Overview of Socket.IO aiohttp example applications

    main

    The examples/server/aiohttp directory provides several demonstration applications compatible with asyncio and the aiohttp framework (requires Python 3.5+):

    • app.py: A comprehensive "kitchen sink" application designed for experimenting with most available Socket.IO server features.
    • latency.py: A port of the official Engine.IO Javascript latency application. It measures the round-trip time of ping and pong messages to help evaluate the performance of different asynchronous modes.
    • fiddle.py: A minimal, simple application based on a JavaScript Socket.IO example.
  3. Explore Socket.IO Client Examples

    main

    The examples/client directory provides several implementation patterns for Socket.IO clients. You can choose an example based on your required concurrency model or language:

    • Synchronous Python: Examples located in the sync directory use standard Python thread concurrency.
    • Asynchronous Python: Examples located in the async directory use Python's asyncio package for concurrency.
    • JavaScript: Examples located in the javascript directory use the JavaScript version of Socket.IO, useful for compatibility testing against Python servers.
  4. Explore Socket.IO ASGI Example Applications

    main

    The examples/server/asgi/ directory contains several reference implementations for testing Socket.IO with asyncio and the ASGI specification:

    • app.py: A comprehensive "kitchen sink" application designed for experimenting with most available Socket.IO server features.
    • latency.py: A performance testing application ported from the official Engine.IO Javascript server. It measures the round-trip time of ping and pong messages to evaluate different asynchronous modes.
    • fiddle.py: A simple, minimal application based on a standard JavaScript example.
    • fastapi-fiddle.py: A version of the fiddle.py application integrated specifically with the FastAPI framework.
    • litestar-fiddle.py: A version of the fiddle.py application integrated specifically with the Litestar framework.
  5. Explore Socket.IO Server Examples by Framework

    main

    The examples/server directory provides reference implementations of Socket.IO servers categorized by the underlying web protocol or framework. Use these examples to understand how to integrate python-socketio with your specific environment:

    • WSGI: For synchronous frameworks (e.g., Flask, Django).
    • ASGI: For asynchronous servers following the ASGI specification (e.g., Daphne, Uvicorn).
    • aiohttp: Specifically for the aiohttp framework.
    • Sanic: Specifically for the sanic framework.
    • Tornado: Specifically for the tornado framework.
    • JavaScript: Client-side examples using the JavaScript version of Socket.IO for compatibility testing.
  6. Deploy Socket.IO with Uvicorn (ASGI)

    main

    For asynchronous Socket.IO servers, use an ASGI web server like Uvicorn. Wrap a socketio.AsyncServer instance with socketio.ASGIApp.

    You can bundle Socket.IO with an existing ASGI application (like FastAPI) by passing it as the second argument to socketio.ASGIApp.

  7. Enable Socket.IO Admin UI

    main

    You can enable the official Socket.IO Admin UI to monitor connected clients, rooms, and events in real-time. This feature is disabled by default due to performance implications. To enable it, call the instrument() method on your server instance. When using the hosted version (https://admin.socket.io), you must also configure cors_allowed_origins to allow the Admin UI to connect.

    import os
    import socketio
    
    sio = socketio.Server(cors_allowed_origins=[
        'http://localhost:5000',
        'https://admin.socket.io',
    ])
    sio.instrument(auth={
        'username': 'admin',
        'password': os.environ['ADMIN_PASSWORD'],
    })