.\" Man page generated from reStructuredText. . .TH "PYTHON-ENGINEIO" "1" "Nov 26, 2018" "" "python-engineio" .SH NAME python-engineio \- python-engineio Documentation . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .sp This project implements an Engine.IO server that can run standalone or integrated with a variety of Python web frameworks. .SH GETTING STARTED .SS What is Engine.IO? .sp Engine.IO is a lightweight transport protocol that enables real\-time bidirectional event\-based communication between clients (typically web browsers) and a server. The official implementations of the client and server components are written in JavaScript. .sp The Engine.IO protocol is extremely simple. The example that follows shows the client\-side Javascript code required to setup an Engine.IO connection to a server: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C var socket = eio(\(aqhttp://chat.example.com\(aq); socket.on(\(aqopen\(aq, function() { alert(\(aqconnected\(aq); }); socket.on(\(aqmessage\(aq, function(data) { alert(data); }); socket.on(\(aqclose\(aq, function() { alert(\(aqdisconnected\(aq); }); socket.send(\(aqHello from the client!\(aq); .ft P .fi .UNINDENT .UNINDENT .SS Features .INDENT 0.0 .IP \(bu 2 Fully compatible with the Javascript \fI\%engine.io\-client\fP library, and with other Engine.IO clients. .IP \(bu 2 Compatible with Python 2.7 and Python 3.3+. .IP \(bu 2 Supports large number of clients even on modest hardware due to being asynchronous. .IP \(bu 2 Compatible with \fI\%aiohttp\fP, \fI\%sanic\fP, \fI\%tornado\fP, \fI\%eventlet\fP, \fI\%gevent\fP, or any \fI\%WSGI\fP or \fI\%ASGI\fP compatible server. .IP \(bu 2 Includes WSGI and ASGI middlewares that integrate Engine.IO traffic with other web applications. .IP \(bu 2 Uses an event\-based architecture implemented with decorators that hides the details of the protocol. .IP \(bu 2 Implements HTTP long\-polling and WebSocket transports. .IP \(bu 2 Supports XHR2 and XHR browsers as clients. .IP \(bu 2 Supports text and binary messages. .IP \(bu 2 Supports gzip and deflate HTTP compression. .IP \(bu 2 Configurable CORS responses to avoid cross\-origin problems with browsers. .UNINDENT .SS Examples .sp The following application is a basic example that uses the Eventlet asynchronous server and includes a small Flask application that serves the HTML/Javascript to the client: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import engineio import eventlet from flask import Flask, render_template eio = engineio.Server() app = Flask(__name__) @app.route(\(aq/\(aq) def index(): """Serve the client\-side application.""" return render_template(\(aqindex.html\(aq) @eio.on(\(aqconnect\(aq) def connect(sid, environ): print("connect ", sid) @eio.on(\(aqmessage\(aq) def message(sid, data): print("message ", data) eio.send(sid, \(aqreply\(aq) @eio.on(\(aqdisconnect\(aq) def disconnect(sid): print(\(aqdisconnect \(aq, sid) if __name__ == \(aq__main__\(aq: # wrap Flask application with engineio\(aqs middleware app = engineio.Middleware(eio, app) # deploy as an eventlet WSGI server eventlet.wsgi.server(eventlet.listen((\(aq\(aq, 8000)), app) .ft P .fi .UNINDENT .UNINDENT .sp Below is a similar application, coded for asyncio (Python 3.5+ only) with the aiohttp framework: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C from aiohttp import web import engineio eio = engineio.AsyncServer() app = web.Application() # attach the Engine.IO server to the application eio.attach(app) async def index(request): """Serve the client\-side application.""" with open(\(aqindex.html\(aq) as f: return web.Response(text=f.read(), content_type=\(aqtext/html\(aq) @eio.on(\(aqconnect\(aq) def connect(sid, environ): print("connect ", sid) @eio.on(\(aqmessage\(aq) async def message(sid, data): print("message ", data) await eio.send(sid, \(aqreply\(aq) @eio.on(\(aqdisconnect\(aq) def disconnect(sid): print(\(aqdisconnect \(aq, sid) app.router.add_static(\(aq/static\(aq, \(aqstatic\(aq) app.router.add_get(\(aq/\(aq, index) if __name__ == \(aq__main__\(aq: # run the aiohttp application web.run_app(app) .ft P .fi .UNINDENT .UNINDENT .sp The client\-side application must include the \fI\%engine.io\-client\fP library (version 1.5.0 or newer recommended). .sp Each time a client connects to the server the \fBconnect\fP event handler is invoked with the \fBsid\fP (session ID) assigned to the connection and the WSGI environment dictionary. The server can inspect authentication or other headers to decide if the client is allowed to connect. To reject a client the handler must return \fBFalse\fP\&. .sp When the client sends a message to the server the \fBmessage\fP event handler is invoked with the \fBsid\fP and the message. .sp Finally, when the connection is broken, the \fBdisconnect\fP event is called, allowing the application to perform cleanup. .sp Because Engine.IO is a bidirectional protocol, the server can send messages to any connected client at any time. The \fBengineio.Server.send()\fP method takes the client\(aqs \fBsid\fP and the message payload, which can be of type \fBstr\fP, \fBbytes\fP, \fBlist\fP or \fBdict\fP (the last two are JSON encoded). .SH DEPLOYMENT .sp The following sections describe a variety of deployment strategies for Engine.IO servers. .SS aiohttp .sp \fI\%aiohttp\fP provides a framework with support for HTTP and WebSocket, based on asyncio. Support for this framework is limited to Python 3.5 and newer. .sp Instances of class \fBengineio.AsyncServer\fP will automatically use aiohttp for asynchronous operations if the library is installed. To request its use explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.AsyncServer(async_mode=\(aqaiohttp\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A server configured for aiohttp must be attached to an existing application: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C app = web.Application() eio.attach(app) .ft P .fi .UNINDENT .UNINDENT .sp The aiohttp application can define regular routes that will coexist with the Engine.IO server. A typical pattern is to add routes that serve a client application and any associated static files. .sp The aiohttp application is then executed in the usual manner: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if __name__ == \(aq__main__\(aq: web.run_app(app) .ft P .fi .UNINDENT .UNINDENT .SS Tornado .sp \fI\%Tornado\fP is a web framework with support for HTTP and WebSocket. Support for this framework requires Python 3.5 and newer. Only Tornado version 5 and newer are supported, thanks to its tight integration with asyncio. .sp Instances of class \fBengineio.AsyncServer\fP will automatically use tornado for asynchronous operations if the library is installed. To request its use explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.AsyncServer(async_mode=\(aqtornado\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A server configured for tornado must include a request handler for Engine.IO: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C app = tornado.web.Application( [ (r"/engine.io/", engineio.get_tornado_handler(eio)), ], # ... other application options ) .ft P .fi .UNINDENT .UNINDENT .sp The tornado application can define other routes that will coexist with the Engine.IO server. A typical pattern is to add routes that serve a client application and any associated static files. .sp The tornado application is then executed in the usual manner: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C app.listen(port) tornado.ioloop.IOLoop.current().start() .ft P .fi .UNINDENT .UNINDENT .SS Sanic .sp \fI\%Sanic\fP is a very efficient asynchronous web server for Python 3.5 and newer. .sp Instances of class \fBengineio.AsyncServer\fP will automatically use Sanic for asynchronous operations if the framework is installed. To request its use explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.AsyncServer(async_mode=\(aqsanic\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A server configured for Sanic must be attached to an existing application: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C app = Sanic() eio.attach(app) .ft P .fi .UNINDENT .UNINDENT .sp The Sanic application can define regular routes that will coexist with the Engine.IO server. A typical pattern is to add routes that serve a client application and any associated static files to this application. .sp The Sanic application is then executed in the usual manner: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if __name__ == \(aq__main__\(aq: app.run() .ft P .fi .UNINDENT .UNINDENT .SS Uvicorn, Daphne, and other ASGI servers .sp The \fBengineio.ASGIApp\fP class is an ASGI compatible application that can forward Engine.IO traffic to an \fBengineio.AsyncServer\fP instance: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.AsyncServer(async_mode=\(aqasgi\(aq) app = engineio.ASGIApp(eio) .ft P .fi .UNINDENT .UNINDENT .sp The application can then be deployed with any ASGI compatible web server. .SS Eventlet .sp \fI\%Eventlet\fP is a high performance concurrent networking library for Python 2 and 3 that uses coroutines, enabling code to be written in the same style used with the blocking standard library functions. An Engine.IO server deployed with eventlet has access to the long\-polling and WebSocket transports. .sp Instances of class \fBengineio.Server\fP will automatically use eventlet for asynchronous operations if the library is installed. To request its use explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.Server(async_mode=\(aqeventlet\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A server configured for eventlet is deployed as a regular WSGI application, using the provided \fBengineio.Middleware\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C app = engineio.Middleware(eio) import eventlet eventlet.wsgi.server(eventlet.listen((\(aq\(aq, 8000)), app) .ft P .fi .UNINDENT .UNINDENT .SS Using Gunicorn with Eventlet .sp An alternative to running the eventlet WSGI server as above is to use \fI\%gunicorn\fP, a fully featured pure Python web server. The command to launch the application under gunicorn is shown below: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ gunicorn \-k eventlet \-w 1 module:app .ft P .fi .UNINDENT .UNINDENT .sp Due to limitations in its load balancing algorithm, gunicorn can only be used with one worker process, so the \fB\-w 1\fP option is required. Note that a single eventlet worker can handle a large number of concurrent clients. .sp Another limitation when using gunicorn is that the WebSocket transport is not available, because this transport it requires extensions to the WSGI standard. .sp Note: Eventlet provides a \fBmonkey_patch()\fP function that replaces all the blocking functions in the standard library with equivalent asynchronous versions. While python\-engineio does not require monkey patching, other libraries such as database drivers are likely to require it. .SS Gevent .sp \fI\%Gevent\fP is another asynchronous framework based on coroutines, very similar to eventlet. An Engine.IO server deployed with gevent has access to the long\-polling transport. If project \fI\%gevent\-websocket\fP is installed, the WebSocket transport is also available. Note that when using the uWSGI server, the native WebSocket implementation of uWSGI can be used instead of gevent\-websocket (see next section for details on this). .sp Instances of class \fBengineio.Server\fP will automatically use gevent for asynchronous operations if the library is installed and eventlet is not installed. To request gevent to be selected explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # gevent alone or with gevent\-websocket eio = engineio.Server(async_mode=\(aqgevent\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A server configured for gevent is deployed as a regular WSGI application, using the provided \fBengineio.Middleware\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C from gevent import pywsgi app = engineio.Middleware(eio) pywsgi.WSGIServer((\(aq\(aq, 8000), app).serve_forever() .ft P .fi .UNINDENT .UNINDENT .sp If the WebSocket transport is installed, then the server must be started as follows: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler app = engineio.Middleware(eio) pywsgi.WSGIServer((\(aq\(aq, 8000), app, handler_class=WebSocketHandler).serve_forever() .ft P .fi .UNINDENT .UNINDENT .SS Using Gunicorn with Gevent .sp An alternative to running the gevent WSGI server as above is to use \fI\%gunicorn\fP, a fully featured pure Python web server. The command to launch the application under gunicorn is shown below: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ gunicorn \-k gevent \-w 1 module:app .ft P .fi .UNINDENT .UNINDENT .sp Or to include WebSocket: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ gunicorn \-k geventwebsocket.gunicorn.workers.GeventWebSocketWorker \-w 1 module: app .ft P .fi .UNINDENT .UNINDENT .sp Same as with eventlet, due to limitations in its load balancing algorithm, gunicorn can only be used with one worker process, so the \fB\-w 1\fP option is required. Note that a single gevent worker can handle a large number of concurrent clients. .sp Note: Gevent provides a \fBmonkey_patch()\fP function that replaces all the blocking functions in the standard library with equivalent asynchronous versions. While python\-engineio does not require monkey patching, other libraries such as database drivers are likely to require it. .SS uWSGI .sp When using the uWSGI server in combination with gevent, the Engine.IO server can take advantage of uWSGI\(aqs native WebSocket support. .sp Instances of class \fBengineio.Server\fP will automatically use this option for asynchronous operations if both gevent and uWSGI are installed and eventlet is not installed. To request this asynchoronous mode explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # gevent with uWSGI eio = engineio.Server(async_mode=\(aqgevent_uwsgi\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A complete explanation of the configuration and usage of the uWSGI server is beyond the scope of this documentation. The uWSGI server is a fairly complex package that provides a large and comprehensive set of options. It must be compiled with WebSocket and SSL support for the WebSocket transport to be available. As way of an introduction, the following command starts a uWSGI server for the \fBlatency.py\fP example on port 5000: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ uwsgi \-\-http :5000 \-\-gevent 1000 \-\-http\-websockets \-\-master \-\-wsgi\-file latency.py \-\-callable app .ft P .fi .UNINDENT .UNINDENT .SS Standard Threads .sp While not comparable to eventlet and gevent in terms of performance, the Engine.IO server can also be configured to work with multi\-threaded web servers that use standard Python threads. This is an ideal setup to use with development servers such as \fI\%Werkzeug\fP\&. Only the long\-polling transport is currently available when using standard threads. .sp Instances of class \fBengineio.Server\fP will automatically use the threading mode if neither eventlet nor gevent are not installed. To request the threading mode explicitly, the \fBasync_mode\fP option can be given in the constructor: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.Server(async_mode=\(aqthreading\(aq) .ft P .fi .UNINDENT .UNINDENT .sp A server configured for threading is deployed as a regular web application, using any WSGI complaint multi\-threaded server. The example below deploys an Engine.IO application combined with a Flask web application, using Flask\(aqs development web server based on Werkzeug: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C eio = engineio.Server(async_mode=\(aqthreading\(aq) app = Flask(__name__) app.wsgi_app = engineio.Middleware(eio, app.wsgi_app) # ... Engine.IO and Flask handler functions ... if __name__ == \(aq__main__\(aq: app.run(threaded=True) .ft P .fi .UNINDENT .UNINDENT .sp When using the threading mode, it is important to ensure that the WSGI server can handle multiple concurrent requests using threads, since a client can have up to two outstanding requests at any given time. The Werkzeug server is single\-threaded by default, so the \fBthreaded=True\fP option is required. .sp Note that servers that use worker processes instead of threads, such as gunicorn, do not support an Engine.IO server configured in threading mode. .SS Scalability Notes .sp Engine.IO is a stateful protocol, which makes horizontal scaling more difficult. To deploy a cluster of Engine.IO processes hosted on one or multiple servers the following conditions must be met: .INDENT 0.0 .IP \(bu 2 Each Engine.IO server process must be able to handle multiple requests concurrently. This is required because long\-polling clients send two requests in parallel. Worker processes that can only handle one request at a time are not supported. .IP \(bu 2 The load balancer must be configured to always forward requests from a client to the same process. Load balancers call this \fIsticky sessions\fP, or \fIsession affinity\fP\&. .UNINDENT .SH API REFERENCE .SS \fBServer\fP class .INDENT 0.0 .TP .B class engineio.Server(async_mode=None, ping_timeout=60, ping_interval=25, max_http_buffer_size=100000000, allow_upgrades=True, http_compression=True, compression_threshold=1024, cookie=\(aqio\(aq, cors_allowed_origins=None, cors_credentials=True, logger=False, json=None, async_handlers=True, monitor_clients=None, **kwargs) An Engine.IO server. .sp This class implements a fully compliant Engine.IO web server with support for websocket and long\-polling transports. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBasync_mode\fP \-\- The asynchronous model to use. See the Deployment section in the documentation for a description of the available options. Valid async modes are "threading", "eventlet", "gevent" and "gevent_uwsgi". If this argument is not given, "eventlet" is tried first, then "gevent_uwsgi", then "gevent", and finally "threading". The first async mode that has all its dependencies installed is then one that is chosen. .IP \(bu 2 \fBping_timeout\fP \-\- The time in seconds that the client waits for the server to respond before disconnecting. The default is 60 seconds. .IP \(bu 2 \fBping_interval\fP \-\- The interval in seconds at which the client pings the server. The default is 25 seconds. .IP \(bu 2 \fBmax_http_buffer_size\fP \-\- The maximum size of a message when using the polling transport. The default is 100,000,000 bytes. .IP \(bu 2 \fBallow_upgrades\fP \-\- Whether to allow transport upgrades or not. The default is \fBTrue\fP\&. .IP \(bu 2 \fBhttp_compression\fP \-\- Whether to compress packages when using the polling transport. The default is \fBTrue\fP\&. .IP \(bu 2 \fBcompression_threshold\fP \-\- Only compress messages when their byte size is greater than this value. The default is 1024 bytes. .IP \(bu 2 \fBcookie\fP \-\- Name of the HTTP cookie that contains the client session id. If set to \fBNone\fP, a cookie is not sent to the client. The default is \fB\(aqio\(aq\fP\&. .IP \(bu 2 \fBcors_allowed_origins\fP \-\- Origin or list of origins that are allowed to connect to this server. All origins are allowed by default, which is equivalent to setting this argument to \fB\(aq*\(aq\fP\&. .IP \(bu 2 \fBcors_credentials\fP \-\- Whether credentials (cookies, authentication) are allowed in requests to this server. The default is \fBTrue\fP\&. .IP \(bu 2 \fBlogger\fP \-\- To enable logging set to \fBTrue\fP or pass a logger object to use. To disable logging set to \fBFalse\fP\&. The default is \fBFalse\fP\&. .IP \(bu 2 \fBjson\fP \-\- An alternative json module to use for encoding and decoding packets. Custom json modules must have \fBdumps\fP and \fBloads\fP functions that are compatible with the standard library versions. .IP \(bu 2 \fBasync_handlers\fP \-\- If set to \fBTrue\fP, run message event handlers in non\-blocking threads. To run handlers synchronously, set to \fBFalse\fP\&. The default is \fBTrue\fP\&. .IP \(bu 2 \fBmonitor_clients\fP \-\- If set to \fBTrue\fP, a background task will ensure inactive clients are closed. Set to \fBFalse\fP to disable the monitoring task (not recommended). The default is \fBTrue\fP\&. .IP \(bu 2 \fBkwargs\fP \-\- Reserved for future extensions, any additional parameters given as keyword arguments will be silently ignored. .UNINDENT .UNINDENT .INDENT 7.0 .TP .B disconnect(sid=None) Disconnect a client. .INDENT 7.0 .TP .B Parameters \fBsid\fP \-\- The session id of the client to close. If this parameter is not given, then all clients are closed. .UNINDENT .UNINDENT .INDENT 7.0 .TP .B handle_request(environ, start_response) Handle an HTTP request from the client. .sp This is the entry point of the Engine.IO application, using the same interface as a WSGI application. For the typical usage, this function is invoked by the \fI\%Middleware\fP instance, but it can be invoked directly when the middleware is not used. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBenviron\fP \-\- The WSGI environment. .IP \(bu 2 \fBstart_response\fP \-\- The WSGI \fBstart_response\fP function. .UNINDENT .UNINDENT .sp This function returns the HTTP response body to deliver to the client as a byte sequence. .UNINDENT .INDENT 7.0 .TP .B on(event, handler=None) Register an event handler. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBevent\fP \-\- The event name. Can be \fB\(aqconnect\(aq\fP, \fB\(aqmessage\(aq\fP or \fB\(aqdisconnect\(aq\fP\&. .IP \(bu 2 \fBhandler\fP \-\- The function that should be invoked to handle the event. When this parameter is not given, the method acts as a decorator for the handler function. .UNINDENT .UNINDENT .sp Example usage: .INDENT 7.0 .INDENT 3.5 .sp .nf .ft C # as a decorator: @eio.on(\(aqconnect\(aq) def connect_handler(sid, environ): print(\(aqConnection request\(aq) if environ[\(aqREMOTE_ADDR\(aq] in blacklisted: return False # reject # as a method: def message_handler(sid, msg): print(\(aqReceived message: \(aq, msg) eio.send(sid, \(aqresponse\(aq) eio.on(\(aqmessage\(aq, message_handler) .ft P .fi .UNINDENT .UNINDENT .sp The handler function receives the \fBsid\fP (session ID) for the client as first argument. The \fB\(aqconnect\(aq\fP event handler receives the WSGI environment as a second argument, and can return \fBFalse\fP to reject the connection. The \fB\(aqmessage\(aq\fP handler receives the message payload as a second argument. The \fB\(aqdisconnect\(aq\fP handler does not take a second argument. .UNINDENT .INDENT 7.0 .TP .B send(sid, data, binary=None) Send a message to a client. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBsid\fP \-\- The session id of the recipient client. .IP \(bu 2 \fBdata\fP \-\- The data to send to the client. Data can be of type \fBstr\fP, \fBbytes\fP, \fBlist\fP or \fBdict\fP\&. If a \fBlist\fP or \fBdict\fP, the data will be serialized as JSON. .IP \(bu 2 \fBbinary\fP \-\- \fBTrue\fP to send packet as binary, \fBFalse\fP to send as text. If not given, unicode (Python 2) and str (Python 3) are sent as text, and str (Python 2) and bytes (Python 3) are sent as binary. .UNINDENT .UNINDENT .UNINDENT .INDENT 7.0 .TP .B sleep(seconds=0) Sleep for the requested amount of time using the appropriate async model. .sp This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode. .UNINDENT .INDENT 7.0 .TP .B start_background_task(target, *args, **kwargs) Start a background task using the appropriate async model. .sp This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBtarget\fP \-\- the target function to execute. .IP \(bu 2 \fBargs\fP \-\- arguments to pass to the function. .IP \(bu 2 \fBkwargs\fP \-\- keyword arguments to pass to the function. .UNINDENT .UNINDENT .sp This function returns an object compatible with the \fIThread\fP class in the Python standard library. The \fIstart()\fP method on this object is already called by this function. .UNINDENT .INDENT 7.0 .TP .B transport(sid) Return the name of the transport used by the client. .sp The two possible values returned by this function are \fB\(aqpolling\(aq\fP and \fB\(aqwebsocket\(aq\fP\&. .INDENT 7.0 .TP .B Parameters \fBsid\fP \-\- The session of the client. .UNINDENT .UNINDENT .UNINDENT .SS \fBAsyncServer\fP class .INDENT 0.0 .TP .B class engineio.AsyncServer(async_mode=None, ping_timeout=60, ping_interval=25, max_http_buffer_size=100000000, allow_upgrades=True, http_compression=True, compression_threshold=1024, cookie=\(aqio\(aq, cors_allowed_origins=None, cors_credentials=True, logger=False, json=None, async_handlers=True, monitor_clients=None, **kwargs) An Engine.IO server for asyncio. .sp This class implements a fully compliant Engine.IO web server with support for websocket and long\-polling transports, compatible with the asyncio framework on Python 3.5 or newer. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBasync_mode\fP \-\- The asynchronous model to use. See the Deployment section in the documentation for a description of the available options. Valid async modes are "aiohttp", "sanic", "tornado" and "asgi". If this argument is not given, an async mode is chosen based on the installed packages. .IP \(bu 2 \fBping_timeout\fP \-\- The time in seconds that the client waits for the server to respond before disconnecting. .IP \(bu 2 \fBping_interval\fP \-\- The interval in seconds at which the client pings the server. .IP \(bu 2 \fBmax_http_buffer_size\fP \-\- The maximum size of a message when using the polling transport. .IP \(bu 2 \fBallow_upgrades\fP \-\- Whether to allow transport upgrades or not. .IP \(bu 2 \fBhttp_compression\fP \-\- Whether to compress packages when using the polling transport. .IP \(bu 2 \fBcompression_threshold\fP \-\- Only compress messages when their byte size is greater than this value. .IP \(bu 2 \fBcookie\fP \-\- Name of the HTTP cookie that contains the client session id. If set to \fBNone\fP, a cookie is not sent to the client. .IP \(bu 2 \fBcors_allowed_origins\fP \-\- List of origins that are allowed to connect to this server. All origins are allowed by default. .IP \(bu 2 \fBcors_credentials\fP \-\- Whether credentials (cookies, authentication) are allowed in requests to this server. .IP \(bu 2 \fBlogger\fP \-\- To enable logging set to \fBTrue\fP or pass a logger object to use. To disable logging set to \fBFalse\fP\&. .IP \(bu 2 \fBjson\fP \-\- An alternative json module to use for encoding and decoding packets. Custom json modules must have \fBdumps\fP and \fBloads\fP functions that are compatible with the standard library versions. .IP \(bu 2 \fBasync_handlers\fP \-\- If set to \fBTrue\fP, run message event handlers in non\-blocking threads. To run handlers synchronously, set to \fBFalse\fP\&. The default is \fBTrue\fP\&. .IP \(bu 2 \fBkwargs\fP \-\- Reserved for future extensions, any additional parameters given as keyword arguments will be silently ignored. .UNINDENT .UNINDENT .INDENT 7.0 .TP .B attach(app, engineio_path=\(aqengine.io\(aq) Attach the Engine.IO server to an application. .UNINDENT .INDENT 7.0 .TP .B disconnect(sid=None) Disconnect a client. .INDENT 7.0 .TP .B Parameters \fBsid\fP \-\- The session id of the client to close. If this parameter is not given, then all clients are closed. .UNINDENT .sp Note: this method is a coroutine. .UNINDENT .INDENT 7.0 .TP .B handle_request(*args, **kwargs) Handle an HTTP request from the client. .sp This is the entry point of the Engine.IO application. This function returns the HTTP response to deliver to the client. .sp Note: this method is a coroutine. .UNINDENT .INDENT 7.0 .TP .B on(event, handler=None) Register an event handler. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBevent\fP \-\- The event name. Can be \fB\(aqconnect\(aq\fP, \fB\(aqmessage\(aq\fP or \fB\(aqdisconnect\(aq\fP\&. .IP \(bu 2 \fBhandler\fP \-\- The function that should be invoked to handle the event. When this parameter is not given, the method acts as a decorator for the handler function. .UNINDENT .UNINDENT .sp Example usage: .INDENT 7.0 .INDENT 3.5 .sp .nf .ft C # as a decorator: @eio.on(\(aqconnect\(aq) def connect_handler(sid, environ): print(\(aqConnection request\(aq) if environ[\(aqREMOTE_ADDR\(aq] in blacklisted: return False # reject # as a method: def message_handler(sid, msg): print(\(aqReceived message: \(aq, msg) eio.send(sid, \(aqresponse\(aq) eio.on(\(aqmessage\(aq, message_handler) .ft P .fi .UNINDENT .UNINDENT .sp The handler function receives the \fBsid\fP (session ID) for the client as first argument. The \fB\(aqconnect\(aq\fP event handler receives the WSGI environment as a second argument, and can return \fBFalse\fP to reject the connection. The \fB\(aqmessage\(aq\fP handler receives the message payload as a second argument. The \fB\(aqdisconnect\(aq\fP handler does not take a second argument. .UNINDENT .INDENT 7.0 .TP .B send(sid, data, binary=None) Send a message to a client. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBsid\fP \-\- The session id of the recipient client. .IP \(bu 2 \fBdata\fP \-\- The data to send to the client. Data can be of type \fBstr\fP, \fBbytes\fP, \fBlist\fP or \fBdict\fP\&. If a \fBlist\fP or \fBdict\fP, the data will be serialized as JSON. .IP \(bu 2 \fBbinary\fP \-\- \fBTrue\fP to send packet as binary, \fBFalse\fP to send as text. If not given, unicode (Python 2) and str (Python 3) are sent as text, and str (Python 2) and bytes (Python 3) are sent as binary. .UNINDENT .UNINDENT .sp Note: this method is a coroutine. .UNINDENT .INDENT 7.0 .TP .B sleep(seconds=0) Sleep for the requested amount of time using the appropriate async model. .sp This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode. .sp Note: this method is a coroutine. .UNINDENT .INDENT 7.0 .TP .B start_background_task(target, *args, **kwargs) Start a background task using the appropriate async model. .sp This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBtarget\fP \-\- the target function to execute. .IP \(bu 2 \fBargs\fP \-\- arguments to pass to the function. .IP \(bu 2 \fBkwargs\fP \-\- keyword arguments to pass to the function. .UNINDENT .UNINDENT .sp The return value is a \fBasyncio.Task\fP object. .UNINDENT .INDENT 7.0 .TP .B transport(sid) Return the name of the transport used by the client. .sp The two possible values returned by this function are \fB\(aqpolling\(aq\fP and \fB\(aqwebsocket\(aq\fP\&. .INDENT 7.0 .TP .B Parameters \fBsid\fP \-\- The session of the client. .UNINDENT .UNINDENT .UNINDENT .SS \fBWSGIApp\fP class .INDENT 0.0 .TP .B class engineio.WSGIApp(engineio_app, wsgi_app=None, static_files=None, engineio_path=\(aqengine.io\(aq) WSGI application middleware for Engine.IO. .sp This middleware dispatches traffic to an Engine.IO application, and optionally forwards regular HTTP traffic to a WSGI application, or serve a list of predefined static files to clients. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBengineio_app\fP \-\- The Engine.IO server. .IP \(bu 2 \fBwsgi_app\fP \-\- The WSGI app that receives all other traffic. .IP \(bu 2 \fBstatic_files\fP \-\- A dictionary where the keys are URLs that should be served as static files. For each URL, the value is a dictionary with \fBcontent_type\fP and \fBfilename\fP keys. This option is intended to be used for serving client files during development. .IP \(bu 2 \fBengineio_path\fP \-\- The endpoint where the Engine.IO application should be installed. The default value is appropriate for most cases. .UNINDENT .UNINDENT .sp Example usage: .INDENT 7.0 .INDENT 3.5 .sp .nf .ft C import engineio import eventlet eio = engineio.Server() app = engineio.WSGIApp(eio, static_files={ \(aq/\(aq: {\(aqcontent_type\(aq: \(aqtext/html\(aq, \(aqfilename\(aq: \(aqindex.html\(aq}, \(aq/index.html\(aq: {\(aqcontent_type\(aq: \(aqtext/html\(aq, \(aqfilename\(aq: \(aqindex.html\(aq}, }) eventlet.wsgi.server(eventlet.listen((\(aq\(aq, 8000)), app) .ft P .fi .UNINDENT .UNINDENT .UNINDENT .SS \fBASGIApp\fP class .INDENT 0.0 .TP .B class engineio.ASGIApp(engineio_server, other_asgi_app=None, static_files=None, engineio_path=\(aqengine.io\(aq) ASGI application middleware for Engine.IO. .sp This middleware dispatches traffic to an Engine.IO application, and optionally serve a list of static files to the client or forward regular HTTP traffic to another ASGI application. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBengineio_server\fP \-\- The Engine.IO server. .IP \(bu 2 \fBstatic_files\fP \-\- A dictionary where the keys are URLs that should be served as static files. For each URL, the value is a dictionary with \fBcontent_type\fP and \fBfilename\fP keys. This option is intended to be used for serving client files during development. .IP \(bu 2 \fBother_asgi_app\fP \-\- A separate ASGI app that receives all other traffic. .IP \(bu 2 \fBengineio_path\fP \-\- The endpoint where the Engine.IO application should be installed. The default value is appropriate for most cases. .UNINDENT .UNINDENT .sp Example usage: .INDENT 7.0 .INDENT 3.5 .sp .nf .ft C import engineio import uvicorn eio = engineio.Server() app = engineio.ASGIApp(eio, static_files={ \(aq/\(aq: {\(aqcontent_type\(aq: \(aqtext/html\(aq, \(aqfilename\(aq: \(aqindex.html\(aq}, \(aq/index.html\(aq: {\(aqcontent_type\(aq: \(aqtext/html\(aq, \(aqfilename\(aq: \(aqindex.html\(aq}, }) uvicorn.run(app, \(aq127.0.0.1\(aq, 5000) .ft P .fi .UNINDENT .UNINDENT .UNINDENT .SS \fBMiddleware\fP class (deprecated) .INDENT 0.0 .TP .B class engineio.Middleware(engineio_app, wsgi_app=None, static_files=None, engineio_path=\(aqengine.io\(aq) This class has been renamed to WSGIApp and is now deprecated. .UNINDENT .INDENT 0.0 .IP \(bu 2 genindex .IP \(bu 2 modindex .IP \(bu 2 search .UNINDENT .SH AUTHOR Miguel Grinberg .SH COPYRIGHT 2018, Miguel Grinberg .\" Generated by docutils manpage writer. .