API

WebSockets

class flask_websockets.WebSockets(app=None, match_one=False, patch_app_run=False)
broadcast(message)
init_app(app)
on_close(fun)
on_message(fun_or_str, flags=0)
on_open(fun)

WebSocketApp

class flask_websockets.WebSocketApp(websocket, sockets_manager)

WebSocketMiddleware

class flask_websockets.WebSocketMiddleware(wsgi_app, sockets_manager, socket_app=<class 'flask_websockets.WebSocketApp'>)

Application Globals

flask_websockets.ws

A namespace object that holds the currently active socket connection inside a SocketContext.

This is a werkzeug.local.LocalProxy.

Useful Functions and Classes

flask_websockets.has_socket_context()
class flask_websockets.SocketContext(websocket)
pop(exc=None)
push()
flask_websockets.patch_app_run(app)

This patches flask.Flask.run() to a version which does not directly fail using WebSockets, to quickly test out Flask-Websockets. It does not support all features of flask.Flask.run() and is not recommended for production settings.

For an alternative, please see run(), which is similar but without the debugging capabilites and no reloading.

Thanks @gmyers18, who provided the basic code here: https://github.com/heroku-python/flask-sockets/issues/48#issuecomment-301060798

flask_websockets.run(app, host='', port=5000)