python-socketio
repository·main·Indexed 26 days ago
https://github.com/miguelgrinberg/python-socketioA 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.
What's inside python-socketio
- python-socketio is a Python implementation of the Socket.IO realtime client and server. It allows for bidirectional, low-latency, event-based communication between clients and servers.
Overview of Socket.IO Tornado example applications
mainThe
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.
Overview of Socket.IO aiohttp example applications
mainThe
examples/server/aiohttpdirectory provides several demonstration applications compatible withasyncioand theaiohttpframework (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.
Overview of Socket.IO Sanic example applications
mainThis directory contains example Socket.IO applications compatible with
asyncioand thesanicframework. These applications require Python 3.5 or later.Note on Sanic versions: Sanic versions older than 0.4.0 do not support the WebSocket protocol; on those versions, the only available transport is long-polling.
Overview of Socket.IO Server types
mainThe
python-socketiopackage provides two main server classes:socketio.Server: Compatible with the Python standard library (synchronous).socketio.AsyncServer: Compatible with theasynciopackage (asynchronous/coroutines).
Explore Socket.IO Client Examples
mainThe
examples/clientdirectory 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
syncdirectory use standard Python thread concurrency. - Asynchronous Python: Examples located in the
asyncdirectory use Python'sasynciopackage for concurrency. - JavaScript: Examples located in the
javascriptdirectory use the JavaScript version of Socket.IO, useful for compatibility testing against Python servers.
- Synchronous Python: Examples located in the
Explore Socket.IO ASGI Example Applications
mainThe
examples/server/asgi/directory contains several reference implementations for testing Socket.IO withasyncioand 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.pyapplication integrated specifically with the FastAPI framework. - litestar-fiddle.py: A version of the
fiddle.pyapplication integrated specifically with the Litestar framework.
Explore Socket.IO Server Examples by Framework
mainThe
examples/serverdirectory provides reference implementations of Socket.IO servers categorized by the underlying web protocol or framework. Use these examples to understand how to integratepython-socketiowith 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
aiohttpframework. - Sanic: Specifically for the
sanicframework. - Tornado: Specifically for the
tornadoframework. - JavaScript: Client-side examples using the JavaScript version of Socket.IO for compatibility testing.
Explore Socket.IO example applications
mainTheexamplesdirectory contains various Socket.IO application implementations. You can find server-side implementations in theserverdirectory, and client-side implementations in theclientandsimple-clientdirectories.Run a Socket.IO server as a WSGI application
mainTo run a Socket.IO server using the WSGI standard (e.g., withGunicornorWerkzeug), wrap the server instance withsocketio.WSGIApp. You can also wrap an existing web application (like Flask or Django) to route traffic between them.Deploy Socket.IO with Uvicorn (ASGI)
mainFor asynchronous Socket.IO servers, use an ASGI web server like Uvicorn. Wrap a
socketio.AsyncServerinstance withsocketio.ASGIApp.You can bundle Socket.IO with an existing ASGI application (like FastAPI) by passing it as the second argument to
socketio.ASGIApp.Enable Socket.IO Admin UI
mainYou 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 configurecors_allowed_originsto 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'], })