@@ -8,13 +8,17 @@ socketio documentation
88
99:ref: `genindex ` | :ref: `modindex ` | :ref: `search `
1010
11- This project implements an Socket.IO server that can run standalone or
11+ This project implements a Socket.IO server that can run standalone or
1212integrated with a Python WSGI application. The following are some of its
1313features:
1414
15- - Fully compatible with the Javascript
16- `socket.io-client <https://github.com/Automattic/socket.io-client >`_ library,
17- versions 1.3.5 and up.
15+ - Fully compatible with the
16+ `Javascript <https://github.com/Automattic/socket.io-client >`_,
17+ `Swift <https://github.com/socketio/socket.io-client-swift >`_,
18+ `C++ <https://github.com/socketio/socket.io-client-cpp >`_ and
19+ `Java <https://github.com/socketio/socket.io-client-java >`_ official
20+ Socket.IO clients, plus any third party clients that comply with the
21+ Socket.IO specification.
1822- Compatible with Python 2.7 and Python 3.3+.
1923- Supports large number of clients even on modest hardware when used with an
2024 asynchronous server based on `eventlet <http://eventlet.net/ >`_ or
@@ -24,25 +28,33 @@ features:
2428 WSGI applications.
2529- Broadcasting of messages to all connected clients, or to subsets of them
2630 assigned to "rooms".
27- - Uses an event-based architecture implemented with decorators that hides the
28- details of the protocol.
31+ - Optional support for multiple servers, connected through a messaging queue
32+ such as Redis or RabbitMQ.
33+ - Send messages to clients from external processes, such as Celery workers or
34+ auxiliary scripts.
35+ - Event-based architecture implemented with decorators that hides the details
36+ of the protocol.
2937- Support for HTTP long-polling and WebSocket transports.
3038- Support for XHR2 and XHR browsers.
3139- Support for text and binary messages.
3240- Support for gzip and deflate HTTP compression.
33- - Configurable CORS responses to avoid cross-origin problems with browsers.
41+ - Configurable CORS responses, to avoid cross-origin problems with browsers.
3442
3543What is Socket.IO?
3644------------------
3745
3846Socket.IO is a transport protocol that enables real-time bidirectional
3947event-based communication between clients (typically web browsers) and a
40- server. The official implementations of the client and server components are
48+ server. The original implementations of the client and server components are
4149written in JavaScript.
4250
4351Getting Started
4452---------------
4553
54+ The Socket.IO server can be installed with pip::
55+
56+ pip install python-socketio
57+
4658The following is a basic example of a Socket.IO server that uses Flask to
4759deploy the client code to the browser::
4860
@@ -100,8 +112,8 @@ Rooms
100112
101113Because Socket.IO is a bidirectional protocol, the server can send messages to
102114any connected client at any time. To make it easy to address groups of clients,
103- the application can put clients into rooms, and then address messages to all
104- the clients in a room.
115+ the application can put clients into rooms, and then address messages to the
116+ entire room.
105117
106118When clients first connect, they are assigned to their own rooms, named with
107119the session ID (the ``sid `` argument passed to all event handlers). The
@@ -198,6 +210,66 @@ methods in the :class:`socketio.Server` class.
198210When the ``namespace `` argument is omitted, set to ``None `` or to ``'/' ``, the
199211default namespace, representing the physical connection, is used.
200212
213+ Using a Message Queue
214+ ---------------------
215+
216+ The Socket.IO server owns the socket connections to all the clients, so it is
217+ the only process that can emit events to them. Unfortunately this becomes a
218+ limitation for many applications, as a common need is to emit events to
219+ clients from a different process, like a
220+ `Celery <http://www.celeryproject.org/ >`_ worker, or any other auxiliary
221+ process or script that works in conjunction with the server.
222+
223+ To enable these other processes to emit events, the server can be configured
224+ to listen for externally issued events on a message queue such as
225+ `Redis <http://redis.io/ >`_ or `RabbitMQ <https://www.rabbitmq.com/ >`_.
226+ Processes that need to emit events to client then post these events to the
227+ queue.
228+
229+ Another situation in which the use of a message queue is necessary is with
230+ high traffic applications that work with large number of clients. To support
231+ these clients, it may be necessary to horizontally scale the Socket.IO
232+ server by splitting the client list among multiple server processes. For this
233+ type of installation, the server processes communicate with each other through
234+ ta message queue.
235+
236+ The message queue service needs to be installed and configured separately. By
237+ default, the server uses `Kombu <http://kombu.readthedocs.org/en/latest/ >`_
238+ to access the message queue, so any message queue supported by this package
239+ can be used. Kombu can be installed with pip::
240+
241+ pip install kombu
242+
243+ To configure a Socket.IO server to connect to a message queue, the
244+ ``client_manager `` argument must be passed in the server creation. The
245+ following example instructs the server to connect to a Redis service running
246+ on the same host and on the default port::
247+
248+ redis = socketio.KombuManager('redis://localhost:6379/')
249+ sio = socketio.Server(client_manager=redis)
250+
251+ For a RabbitMQ queue also running on the local server, the configuration is
252+ as follows::
253+
254+ amqp = socketio.KombuManager('amqp://guest:guest@localhost:5672//')
255+ sio = socketio.Server(client_manager=amqp)
256+
257+ The arguments passed to the ``KombuManager `` constructor are passed directly
258+ to Kombu's `Connection object
259+ <http://kombu.readthedocs.org/en/latest/userguide/connections.html> `_.
260+
261+ If multiple Sokcet.IO servers are connected to a message queue, they
262+ automatically communicate with each other and manage a combine client list,
263+ without any need for additional configuration. To have a process other than
264+ the server connect to the queue to emit a message, the same ``KombuManager ``
265+ class can be used. For example::
266+
267+ # connect to the redis queue
268+ redis = socketio.KombuManager('redis://localhost:6379/')
269+
270+ # emit an event
271+ redis.emit('my event', data={'foo': 'bar'}, room='my room')
272+
201273Deployment
202274----------
203275
@@ -233,16 +305,14 @@ command to launch the application under gunicorn is shown below::
233305 $ gunicorn -k eventlet -w 1 module:app
234306
235307Due to limitations in its load balancing algorithm, gunicorn can only be used
236- with one worker process, so the ``-w 1 `` option is required. Note that a
237- single eventlet worker can handle a large number of concurrent clients.
308+ with one worker process, so the ``-w `` option cannot be set to a value higher
309+ than 1. A single eventlet worker can handle a large number of concurrent
310+ clients, each handled by a greenlet.
238311
239- Another limitation when using gunicorn is that the WebSocket transport is not
240- available, because this transport it requires extensions to the WSGI standard.
241-
242- Note: Eventlet provides a ``monkey_patch() `` function that replaces all the
243- blocking functions in the standard library with equivalent asynchronous
244- versions. While python-socketio does not require monkey patching, other
245- libraries such as database drivers are likely to require it.
312+ Eventlet provides a ``monkey_patch() `` function that replaces all the blocking
313+ functions in the standard library with equivalent asynchronous versions. While
314+ python-socketio does not require monkey patching, other libraries such as
315+ database drivers are likely to require it.
246316
247317Gevent
248318~~~~~~
@@ -287,14 +357,14 @@ Or to include WebSocket::
287357 $ gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module: app
288358
289359Same as with eventlet, due to limitations in its load balancing algorithm,
290- gunicorn can only be used with one worker process, so the ``-w 1 `` option is
291- required. Note that a single eventlet worker can handle a large number of
292- concurrent clients.
360+ gunicorn can only be used with one worker process, so the ``-w `` option cannot
361+ be higher than 1. A single gevent worker can handle a large number of
362+ concurrent clients through the use of greenlets .
293363
294- Note: Gevent provides a ``monkey_patch() `` function that replaces all the
295- blocking functions in the standard library with equivalent asynchronous
296- versions. While python-socketio does not require monkey patching, other
297- libraries such as database drivers are likely to require it.
364+ Gevent provides a ``monkey_patch() `` function that replaces all the blocking
365+ functions in the standard library with equivalent asynchronous versions. While
366+ python-socketio does not require monkey patching, other libraries such as
367+ database drivers are likely to require it.
298368
299369Standard Threading Library
300370~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -345,25 +415,25 @@ multiple servers), the following conditions must be met:
345415 using eventlet, gevent, or standard threads. Worker processes that only
346416 handle one request at a time are not supported.
347417- The load balancer must be configured to always forward requests from a
348- client to the same process. Load balancers call this *sticky sessions *, or
349- *session affinity *.
350-
351- A limitation in the current release of the Socket.IO server is that because
352- the clients are randomly assigned to different server processes, any form of
353- broadcasting is not supported. A storage backend that enables multiple
354- processes to share information about clients is currently in development to
355- address this important limitation.
418+ client to the same worker process. Load balancers call this *sticky
419+ sessions *, or *session affinity *.
420+ - The worker processes communicate with each other through a message queue,
421+ which must be installed and configured. See the section on using message
422+ queues above for instructions.
356423
357424API Reference
358425-------------
359426
360- .. toctree ::
361- :maxdepth: 2
362-
363427.. module :: socketio
364-
365428.. autoclass :: Middleware
366429 :members:
367-
368430.. autoclass :: Server
369431 :members:
432+ .. autoclass :: BaseManager
433+ :members:
434+ .. autoclass :: PubSubManager
435+ :members:
436+ .. autoclass :: KombuManager
437+ :members:
438+ .. autoclass :: RedisManager
439+ :members:
0 commit comments