Engine.IO server and client for Python providing real-time bidirectional communication
npx @tessl/cli install tessl/pypi-python-engineio@4.12.0A comprehensive Python implementation of the Engine.IO realtime communication protocol, providing both client and server capabilities for building real-time web applications. Engine.IO enables bidirectional event-based communication between clients and servers with automatic reconnection, transport fallbacks (WebSocket to HTTP long-polling), and seamless integration with multiple async frameworks.
pip install python-engineioimport engineioCommon usage patterns:
# Server applications
from engineio import Server, AsyncServer
from engineio import WSGIApp, ASGIApp
# Client applications
from engineio import Client, AsyncClient
# Exception handling
from engineio.exceptions import EngineIOError, ConnectionErrorimport engineio
# Create synchronous server
eio = engineio.Server()
@eio.on('connect')
def on_connect(sid, environ):
print(f'Client {sid} connected')
@eio.on('message')
def on_message(sid, data):
print(f'Received {data} from {sid}')
eio.send(sid, 'response message')
@eio.on('disconnect')
def on_disconnect(sid):
print(f'Client {sid} disconnected')
# Use with WSGI application
app = engineio.WSGIApp(eio)import engineio
# Create synchronous client
eio = engineio.Client()
@eio.on('connect')
def on_connect():
print('Connected to server')
eio.send('Hello Server!')
@eio.on('message')
def on_message(data):
print(f'Received from server: {data}')
@eio.on('disconnect')
def on_disconnect():
print('Disconnected from server')
# Connect to server
eio.connect('http://localhost:5000')
eio.wait()import engineio
# Create asynchronous server
eio = engineio.AsyncServer()
@eio.on('connect')
async def on_connect(sid, environ):
print(f'Client {sid} connected')
@eio.on('message')
async def on_message(sid, data):
print(f'Received {data} from {sid}')
await eio.send(sid, 'async response')
# Use with ASGI application
app = engineio.ASGIApp(eio)Engine.IO provides a layered architecture for real-time communication:
The library serves as the foundation transport layer for Socket.IO and other real-time communication systems, providing reliable bidirectional messaging with automatic connection management and protocol negotiation.
Full-featured Engine.IO server for synchronous Python applications with support for multiple async models including threading, eventlet, and gevent.
class Server:
def __init__(self, async_mode=None, ping_interval=25, ping_timeout=20, **kwargs): ...
def send(self, sid, data): ...
def disconnect(self, sid=None): ...
def on(self, event, handler=None): ...
def handle_request(self, environ, start_response): ...Engine.IO server optimized for asyncio-based applications with full async/await support and integration with modern async web frameworks.
class AsyncServer:
def __init__(self, async_mode=None, ping_interval=25, ping_timeout=20, **kwargs): ...
async def send(self, sid, data): ...
async def disconnect(self, sid=None): ...
def on(self, event, handler=None): ...
async def handle_request(self, *args, **kwargs): ...Engine.IO client for connecting to servers from synchronous Python applications with automatic reconnection and transport fallback support.
class Client:
def __init__(self, logger=False, request_timeout=5, **kwargs): ...
def connect(self, url, headers=None, transports=None, engineio_path='engine.io'): ...
def send(self, data): ...
def disconnect(self, abort=False, reason=None): ...
def on(self, event, handler=None): ...Asyncio-based Engine.IO client for connecting to servers from async Python applications with full async/await support.
class AsyncClient:
def __init__(self, logger=False, request_timeout=5, **kwargs): ...
async def connect(self, url, headers=None, transports=None, engineio_path='engine.io'): ...
async def send(self, data): ...
async def disconnect(self, abort=False, reason=None): ...
def on(self, event, handler=None): ...WSGI application middleware for integrating Engine.IO servers with Flask, Django, and other WSGI-compatible web frameworks.
class WSGIApp:
def __init__(self, engineio_app, wsgi_app=None, static_files=None, engineio_path='engine.io'): ...
def __call__(self, environ, start_response): ...ASGI application middleware for integrating Engine.IO servers with FastAPI, Starlette, and other ASGI-compatible web frameworks.
class ASGIApp:
def __init__(self, engineio_server, other_asgi_app=None, static_files=None, **kwargs): ...
async def __call__(self, scope, receive, send): ...Tornado WebSocket handler for integrating Engine.IO with Tornado web applications. Returns a dynamically created handler class that extends tornado.websocket.WebSocketHandler.
def get_tornado_handler(engineio_server):
"""
Return a Tornado WebSocket handler class for Engine.IO integration.
Args:
engineio_server (AsyncServer): The Engine.IO async server instance
Returns:
class: Tornado WebSocket handler class that extends WebSocketHandler
Note:
Function may return None if Tornado is not installed.
The returned handler class supports both HTTP and WebSocket requests.
"""Comprehensive exception system for handling Engine.IO-specific errors and connection issues.
class EngineIOError(Exception): ...
class ContentTooLongError(EngineIOError): ...
class UnknownPacketError(EngineIOError): ...
class QueueEmpty(EngineIOError): ...
class SocketIsClosedError(EngineIOError): ...
class ConnectionError(EngineIOError): ...# Session ID type
SessionId = str
# Event handler signature
EventHandler = Callable[[str, Any], None]
AsyncEventHandler = Callable[[str, Any], Awaitable[None]]
# WSGI environment
WSGIEnviron = Dict[str, Any]
# Transport types
Transport = Literal['polling', 'websocket']