Python Engine.IO
Python-Engine.IO is a lightweight transport protocol implementation that facilitates real-time, bidirectional, event-based communication between clients and a server. It provides Python implementations for both the Engine.IO server and client, offering standard (threaded) and asyncio variants. The library is actively maintained, with frequent releases, and is currently at version 4.13.1. It forms the low-level transport layer for Socket.IO.
Warnings
- breaking Engine.IO v4 introduced major breaking changes, including a reversal of the heartbeat (ping/pong) mechanism (server now pings, client responds), a new packet encoding format, and a reduced default `maxHttpBufferSize` (from 100MB to 1MB). Clients running on Engine.IO v3 will NOT be compatible with servers running on v4, and vice-versa.
- gotcha The Engine.IO server library is a protocol implementation and requires an underlying asynchronous framework or WSGI/ASGI web server (e.g., `eventlet`, `gevent`, `aiohttp`, `uvicorn`, `tornado`) to function as a complete web application. It does not provide its own standalone web server.
- gotcha When defining event handlers (e.g., with `@eio.on('message')`), avoid using mutable objects (like lists or dictionaries) as default arguments in the handler function signature. These defaults are created once when the function is defined and shared across all calls/sessions, leading to unexpected behavior.
- gotcha The library internally supports a single custom JSON module per process. Attempting to configure multiple custom JSON encoder/decoder modules might lead to unexpected behavior or errors, as documented in recent changes.
- deprecated Older Python versions (e.g., 3.7, 3.8, 3.9) are being dropped from continuous integration (CI) builds, indicating that while they might still work, active testing and support focus on newer Python versions (3.10+). The `requires_python` specifier is `>=3.8`.
Install
-
pip install python-engineio -
pip install "python-engineio[client]" -
pip install "python-engineio[asyncio_client]" -
pip install "python-engineio[asyncio]" uvicorn -
pip install "python-engineio[eventlet]" eventlet
Imports
- Server
import engineio eio_server = engineio.Server()
- AsyncServer
import engineio eio_server = engineio.AsyncServer()
- Client
import engineio eio_client = engineio.Client()
- AsyncClient
import engineio eio_client = engineio.AsyncClient()
- WSGIApp
from engineio import WSGIApp
- ASGIApp
from engineio import ASGIApp
Quickstart
import engineio
import uvicorn
eio = engineio.AsyncServer(async_mode='asgi')
app = engineio.ASGIApp(eio)
@eio.on('connect')
async def connect(sid, environ):
print('connect ', sid)
@eio.on('message')
async def message(sid, data):
print('message ', data)
await eio.send(sid, 'reply from server')
@eio.on('disconnect')
async def disconnect(sid):
print('disconnect ', sid)
# To run the server (requires uvicorn):
# if __name__ == '__main__':
# uvicorn.run(app, host='127.0.0.1', port=5000)
# --- Client Example (run in a separate process/script) ---
# import engineio
# import asyncio
# eio_client = engineio.AsyncClient()
# @eio_client.on('connect')
# async def on_connect():
# print('client connected')
# await eio_client.send('Hello from client!')
# @eio_client.on('message')
# async def on_message(data):
# print('client received: ', data)
# @eio_client.on('disconnect')
# async def on_disconnect():
# print('client disconnected')
# async def start_client():
# await eio_client.connect('http://localhost:5000')
# await eio_client.wait()
# # To run the client:
# # if __name__ == '__main__':
# # asyncio.run(start_client())