Skip to content

Commit 2582f28

Browse files
websocket support for sanic
1 parent 7e10afe commit 2582f28

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

README.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Features
1919
Socket.IO specification.
2020
- Compatible with Python 2.7 and Python 3.3+.
2121
- Supports large number of clients even on modest hardware when used with an
22-
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_,
22+
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_
23+
(`sanic <http://sanic.readthedocs.io/>`_ and `aiohttp <http://aiohttp.readthedocs.io/>`_),
2324
`eventlet <http://eventlet.net/>`_ or `gevent <http://gevent.org/>`_. For
2425
development and testing, any WSGI complaint multi-threaded server can also be
2526
used.

docs/index.rst

+32-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ features:
1919
Socket.IO specification.
2020
- Compatible with Python 2.7 and Python 3.3+.
2121
- Supports large number of clients even on modest hardware when used with an
22-
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_,
23-
`eventlet <http://eventlet.net/>`_ or `gevent <http://gevent.org/>`_. For
24-
development and testing, any WSGI complaint multi-threaded server can also be
22+
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_
23+
(`sanic <http://sanic.readthedocs.io/>`_ and `aiohttp <http://aiohttp.readthedocs.io/>`_),
24+
`eventlet <http://eventlet.net/>`_ or `gevent <http://gevent.org>`_. For
25+
development and testing, any WSGI compliant multi-threaded server can also be
2526
used.
2627
- Includes a WSGI middleware that integrates Socket.IO traffic with standard
2728
WSGI applications.
@@ -458,14 +459,40 @@ Deployment
458459
The following sections describe a variety of deployment strategies for
459460
Socket.IO servers.
460461

461-
Aiohttp
462+
Sanic
463+
~~~~~
464+
465+
`Sanic <http://sanic.readthedocs.io/>`_ is a very efficient asynchronous web
466+
server for Python 3.5 and newer.
467+
468+
Instances of class ``socketio.AsyncServer`` will automatically use Sanic for
469+
asynchronous operations if the framework is installed. To request its use
470+
explicitly, the ``async_mode`` option can be given in the constructor::
471+
472+
sio = socketio.AsyncServer(async_mode='sanic')
473+
474+
A server configured for aiohttp must be attached to an existing application::
475+
476+
app = web.Application()
477+
sio.attach(app)
478+
479+
The Sanic application can define regular routes that will coexist with the
480+
Socket.IO server. A typical pattern is to add routes that serve a client
481+
application and any associated static files.
482+
483+
The Sanic application is then executed in the usual manner::
484+
485+
if __name__ == '__main__':
486+
app.run()
487+
488+
aiohttp
462489
~~~~~~~
463490

464491
`Aiohttp <http://aiohttp.readthedocs.io/>`_ is a framework with support for HTTP
465492
and WebSocket, based on asyncio. Support for this framework is limited to Python
466493
3.5 and newer.
467494

468-
Instances of class ``engineio.AsyncServer`` will automatically use aiohttp
495+
Instances of class ``socketio.AsyncServer`` will automatically use aiohttp
469496
for asynchronous operations if the library is installed. To request its use
470497
explicitly, the ``async_mode`` option can be given in the constructor::
471498

examples/sanic/README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ This directory contains example Socket.IO applications that are compatible with
55
asyncio and the sanic framework. These applications require Python 3.5 or
66
later.
77

8-
Note that because sanic does not support the WebSocket protocol, the only
9-
transport is long-polling at this time.
8+
Note that Sanic versions older than 0.4.0 do not support the WebSocket
9+
protocol, so on those versions the only available transport is long-polling.
1010

1111
app.py
1212
------

examples/sanic/app.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ async def background_task():
2020
namespace='/test')
2121

2222

23+
@app.listener('before_server_start')
24+
def before_server_start(sanic, loop):
25+
sio.start_background_task(background_task)
26+
27+
2328
@app.route('/')
2429
async def index(request):
2530
with open('app.html') as f:
@@ -85,5 +90,4 @@ def test_disconnect(sid):
8590

8691

8792
if __name__ == '__main__':
88-
sio.start_background_task(background_task)
8993
app.run()

0 commit comments

Comments
 (0)