@@ -8,7 +8,7 @@ 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
@@ -49,6 +49,10 @@ written in JavaScript.
4949Getting Started
5050---------------
5151
52+ The Socket.IO server can be installed with pip::
53+
54+ pip install python-socketio
55+
5256The following is a basic example of a Socket.IO server that uses Flask to
5357deploy the client code to the browser::
5458
@@ -106,8 +110,8 @@ Rooms
106110
107111Because Socket.IO is a bidirectional protocol, the server can send messages to
108112any connected client at any time. To make it easy to address groups of clients,
109- the application can put clients into rooms, and then address messages to all
110- the clients in a room.
113+ the application can put clients into rooms, and then address messages to the
114+ entire room.
111115
112116When clients first connect, they are assigned to their own rooms, named with
113117the session ID (the ``sid `` argument passed to all event handlers). The
@@ -204,6 +208,65 @@ methods in the :class:`socketio.Server` class.
204208When the ``namespace `` argument is omitted, set to ``None `` or to ``'/' ``, the
205209default namespace, representing the physical connection, is used.
206210
211+ Using a Message Queue
212+ ---------------------
213+
214+ The Socket.IO server owns the socket connections to all the clients, so it is
215+ the only process that can emit events to them. A common need of larger
216+ applications is to emit events to clients from a different process, like a
217+ a `Celery <http://www.celeryproject.org/ >`_ worker, or any other auxiliary
218+ process that works in conjunction with the server.
219+
220+ To enable these other processes to emit events, the server can be configured
221+ to listen for events to emit to clients on a message queue such as
222+ `Redis <http://redis.io/ >`_ or `RabbitMQ <https://www.rabbitmq.com/ >`_.
223+ Processes that need to emit events to client then post these events to the
224+ queue.
225+
226+ Another situation in which the use of a message queue is necessary is with
227+ high traffic applications that work with large number of clients. To support
228+ these clients, it may be necessary to horizontally scale the Socket.IO
229+ server by splitting the client list among multiple server processes. For this
230+ type of installation, the server processes communicate with each other through
231+ ta message queue.
232+
233+ The message queue service needs to be installed and configured separately. By
234+ default, the server uses `Kombu <http://kombu.readthedocs.org/en/latest/ >`_
235+ to read and write to the queue, so any message queue supported by this package
236+ can be used. Kombu can be installed with pip::
237+
238+ pip install kombu
239+
240+ To configure a Socket.IO server to connect to a message queue, the
241+ ``client_manager `` argument must be passed in the server creation. The
242+ following example instructs the server to connect to a Redis service running
243+ on the same host and on the default port::
244+
245+ redis = socketio.KombuManager('redis://localhost:6379/')
246+ sio = socketio.Server(client_manager=redis)
247+
248+ For a RabbitMQ queue also running on the local server, the configuration is
249+ as follows::
250+
251+ amqp = socketio.KombuManager('amqp://guest:guest@localhost:5672//')
252+ sio = socketio.Server(client_manager=amqp)
253+
254+ The arguments passed to the ``KombuManager `` constructor are passed directly
255+ to Kombu's `Connection object
256+ <http://kombu.readthedocs.org/en/latest/userguide/connections.html> `_.
257+
258+ If multiple Sokcet.IO servers are connected to a message queue, they
259+ automatically communicate with each other and manage a combine client list,
260+ without any need for additional configuration. To have a process other than
261+ the server connect to the queue to emit a message, the same ``KombuManager ``
262+ class can be used. For example::
263+
264+ # connect to the redis queue
265+ redis = socketio.KombuManager('redis://localhost:6379/')
266+
267+ # emit an event
268+ redis.emit('my event', data={'foo': 'bar'}, room='my room')
269+
207270Deployment
208271----------
209272
@@ -239,16 +302,14 @@ command to launch the application under gunicorn is shown below::
239302 $ gunicorn -k eventlet -w 1 module:app
240303
241304Due to limitations in its load balancing algorithm, gunicorn can only be used
242- with one worker process, so the ``-w 1 `` option is required. Note that a
243- single eventlet worker can handle a large number of concurrent clients.
244-
245- Another limitation when using gunicorn is that the WebSocket transport is not
246- available, because this transport it requires extensions to the WSGI standard.
305+ with one worker process, so the ``-w `` option cannot be set to a value higher
306+ than 1. A single eventlet worker can handle a large number of concurrent
307+ clients, each handled by a greenlet.
247308
248- Note: Eventlet provides a ``monkey_patch() `` function that replaces all the
249- blocking functions in the standard library with equivalent asynchronous
250- versions. While python-socketio does not require monkey patching, other
251- libraries such as database drivers are likely to require it.
309+ Eventlet provides a ``monkey_patch() `` function that replaces all the blocking
310+ functions in the standard library with equivalent asynchronous versions. While
311+ python-socketio does not require monkey patching, other libraries such as
312+ database drivers are likely to require it.
252313
253314Gevent
254315~~~~~~
@@ -293,14 +354,14 @@ Or to include WebSocket::
293354 $ gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module: app
294355
295356Same as with eventlet, due to limitations in its load balancing algorithm,
296- gunicorn can only be used with one worker process, so the ``-w 1 `` option is
297- required. Note that a single eventlet worker can handle a large number of
298- concurrent clients.
357+ gunicorn can only be used with one worker process, so the ``-w `` option cannot
358+ be higher than 1. A single gevent worker can handle a large number of
359+ concurrent clients through the use of greenlets .
299360
300- Note: Gevent provides a ``monkey_patch() `` function that replaces all the
301- blocking functions in the standard library with equivalent asynchronous
302- versions. While python-socketio does not require monkey patching, other
303- libraries such as database drivers are likely to require it.
361+ Gevent provides a ``monkey_patch() `` function that replaces all the blocking
362+ functions in the standard library with equivalent asynchronous versions. While
363+ python-socketio does not require monkey patching, other libraries such as
364+ database drivers are likely to require it.
304365
305366Standard Threading Library
306367~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -351,14 +412,11 @@ multiple servers), the following conditions must be met:
351412 using eventlet, gevent, or standard threads. Worker processes that only
352413 handle one request at a time are not supported.
353414- The load balancer must be configured to always forward requests from a
354- client to the same process. Load balancers call this *sticky sessions *, or
355- *session affinity *.
356-
357- A limitation in the current release of the Socket.IO server is that because
358- the clients are randomly assigned to different server processes, any form of
359- broadcasting is not supported. A storage backend that enables multiple
360- processes to share information about clients is currently in development to
361- address this important limitation.
415+ client to the same worker process. Load balancers call this *sticky
416+ sessions *, or *session affinity *.
417+ - The worker processes communicate with each other through a message queue,
418+ which must be installed and configured. See the section on using message
419+ queues above for instructions.
362420
363421API Reference
364422-------------
0 commit comments