Python Engine.IO

4.13.1 · active · verified Sun Apr 05

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

Install

Imports

Quickstart

This quickstart demonstrates a basic Engine.IO server using the `AsyncServer` with ASGI mode, integrated with `uvicorn`. It defines event handlers for connect, message, and disconnect. A corresponding `AsyncClient` example is commented out, showing how to connect to the server and send/receive messages. Remember to install `uvicorn` and run the server and client in separate processes.

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())

view raw JSON →