Skip to content

Commit

Permalink
websocket support for sanic
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Mar 8, 2017
1 parent 7e10afe commit 2582f28
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Features
Socket.IO specification.
- Compatible with Python 2.7 and Python 3.3+.
- Supports large number of clients even on modest hardware when used with an
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_,
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_
(`sanic <http://sanic.readthedocs.io/>`_ and `aiohttp <http://aiohttp.readthedocs.io/>`_),
`eventlet <http://eventlet.net/>`_ or `gevent <http://gevent.org/>`_. For
development and testing, any WSGI complaint multi-threaded server can also be
used.
Expand Down
37 changes: 32 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ features:
Socket.IO specification.
- Compatible with Python 2.7 and Python 3.3+.
- Supports large number of clients even on modest hardware when used with an
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_,
`eventlet <http://eventlet.net/>`_ or `gevent <http://gevent.org/>`_. For
development and testing, any WSGI complaint multi-threaded server can also be
asynchronous server based on `asyncio <https://docs.python.org/3/library/asyncio.html>`_
(`sanic <http://sanic.readthedocs.io/>`_ and `aiohttp <http://aiohttp.readthedocs.io/>`_),
`eventlet <http://eventlet.net/>`_ or `gevent <http://gevent.org>`_. For
development and testing, any WSGI compliant multi-threaded server can also be
used.
- Includes a WSGI middleware that integrates Socket.IO traffic with standard
WSGI applications.
Expand Down Expand Up @@ -458,14 +459,40 @@ Deployment
The following sections describe a variety of deployment strategies for
Socket.IO servers.

Aiohttp
Sanic
~~~~~

`Sanic <http://sanic.readthedocs.io/>`_ is a very efficient asynchronous web
server for Python 3.5 and newer.

Instances of class ``socketio.AsyncServer`` will automatically use Sanic for
asynchronous operations if the framework is installed. To request its use
explicitly, the ``async_mode`` option can be given in the constructor::

sio = socketio.AsyncServer(async_mode='sanic')

A server configured for aiohttp must be attached to an existing application::

app = web.Application()
sio.attach(app)

The Sanic application can define regular routes that will coexist with the
Socket.IO server. A typical pattern is to add routes that serve a client
application and any associated static files.

The Sanic application is then executed in the usual manner::

if __name__ == '__main__':
app.run()

aiohttp
~~~~~~~

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

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

Expand Down
4 changes: 2 additions & 2 deletions examples/sanic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ This directory contains example Socket.IO applications that are compatible with
asyncio and the sanic framework. These applications require Python 3.5 or
later.

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

app.py
------
Expand Down
6 changes: 5 additions & 1 deletion examples/sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ async def background_task():
namespace='/test')


@app.listener('before_server_start')
def before_server_start(sanic, loop):
sio.start_background_task(background_task)


@app.route('/')
async def index(request):
with open('app.html') as f:
Expand Down Expand Up @@ -85,5 +90,4 @@ def test_disconnect(sid):


if __name__ == '__main__':
sio.start_background_task(background_task)
app.run()

0 comments on commit 2582f28

Please sign in to comment.