API Reference¶
- class flask_sock.Sock(app=None)¶
Instantiate the Flask-Sock extension.
- Parameters:
app – The Flask application instance. If not provided, it must be initialized later by calling the
Sock.init_app()
method.
- init_app(app)¶
Initialize the Flask-Socket extension.
- Parameters:
app – The Flask application instance. This method only needs to be called if the application instance was not passed as an argument to the constructor.
- route(path, bp=None, **kwargs)¶
Decorator to create a WebSocket route.
The decorated function will be invoked when a WebSocket client establishes a connection, with a WebSocket connection object passed as an argument. Example:
@sock.route('/ws') def websocket_route(ws): # The ws object has the following methods: # - ws.send(data) # - ws.receive(timeout=None) # - ws.close(reason=None, message=None)
If the route has variable components, the
ws
argument needs to be included before them.- Parameters:
path – the URL associated with the route.
bp – the blueprint on which to register the route. If not given, the route is attached directly to the Flask application instance. When a blueprint is used, the application is responsible for the blueprint’s registration.
kwargs – additional route options. See the Flask documentation for the
app.route
decorator for details.