WebSocket¶
Sanic supports websockets, to setup a WebSocket:
- from sanic import Sanic
- from sanic.response import json
- from sanic.websocket import WebSocketProtocol
- app = Sanic()
- @app.websocket('/feed')
- async def feed(request, ws):
- while True:
- data = 'hello!'
- print('Sending: ' + data)
- await ws.send(data)
- data = await ws.recv()
- print('Received: ' + data)
- if __name__ == "__main__":
- app.run(host="0.0.0.0", port=8000, protocol=WebSocketProtocol)
Alternatively, the app.add_websocket_route
method can be used instead of the decorator:
- async def feed(request, ws):
- pass
- app.add_websocket_route(feed, '/feed')
Handlers for a WebSocket route are passed the request as first argument, and a WebSocket protocol object as second argument. The protocol object has send
and recv
methods to send and receive data respectively.
You could setup your own WebSocket configuration through app.config
, like
Find more in Configuration
section.