@@ -8,13 +8,17 @@ socketio documentation
8
8
9
9
:ref: `genindex ` | :ref: `modindex ` | :ref: `search `
10
10
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
12
12
integrated with a Python WSGI application. The following are some of its
13
13
features:
14
14
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.
18
22
- Compatible with Python 2.7 and Python 3.3+.
19
23
- Supports large number of clients even on modest hardware when used with an
20
24
asynchronous server based on `eventlet <http://eventlet.net/ >`_ or
@@ -24,25 +28,33 @@ features:
24
28
WSGI applications.
25
29
- Broadcasting of messages to all connected clients, or to subsets of them
26
30
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.
29
37
- Support for HTTP long-polling and WebSocket transports.
30
38
- Support for XHR2 and XHR browsers.
31
39
- Support for text and binary messages.
32
40
- 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.
34
42
35
43
What is Socket.IO?
36
44
------------------
37
45
38
46
Socket.IO is a transport protocol that enables real-time bidirectional
39
47
event-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
41
49
written in JavaScript.
42
50
43
51
Getting Started
44
52
---------------
45
53
54
+ The Socket.IO server can be installed with pip::
55
+
56
+ pip install python-socketio
57
+
46
58
The following is a basic example of a Socket.IO server that uses Flask to
47
59
deploy the client code to the browser::
48
60
@@ -100,8 +112,8 @@ Rooms
100
112
101
113
Because Socket.IO is a bidirectional protocol, the server can send messages to
102
114
any 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.
105
117
106
118
When clients first connect, they are assigned to their own rooms, named with
107
119
the session ID (the ``sid `` argument passed to all event handlers). The
@@ -198,6 +210,66 @@ methods in the :class:`socketio.Server` class.
198
210
When the ``namespace `` argument is omitted, set to ``None `` or to ``'/' ``, the
199
211
default namespace, representing the physical connection, is used.
200
212
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
+
201
273
Deployment
202
274
----------
203
275
@@ -233,16 +305,14 @@ command to launch the application under gunicorn is shown below::
233
305
$ gunicorn -k eventlet -w 1 module:app
234
306
235
307
Due 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.
238
311
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.
246
316
247
317
Gevent
248
318
~~~~~~
@@ -287,14 +357,14 @@ Or to include WebSocket::
287
357
$ gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module: app
288
358
289
359
Same 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 .
293
363
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.
298
368
299
369
Standard Threading Library
300
370
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -345,25 +415,25 @@ multiple servers), the following conditions must be met:
345
415
using eventlet, gevent, or standard threads. Worker processes that only
346
416
handle one request at a time are not supported.
347
417
- 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.
356
423
357
424
API Reference
358
425
-------------
359
426
360
- .. toctree ::
361
- :maxdepth: 2
362
-
363
427
.. module :: socketio
364
-
365
428
.. autoclass :: Middleware
366
429
:members:
367
-
368
430
.. autoclass :: Server
369
431
:members:
432
+ .. autoclass :: BaseManager
433
+ :members:
434
+ .. autoclass :: PubSubManager
435
+ :members:
436
+ .. autoclass :: KombuManager
437
+ :members:
438
+ .. autoclass :: RedisManager
439
+ :members:
0 commit comments