Skip to content

Commit

Permalink
Pass WebSocket server options in the configuration (Fixes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Mar 17, 2022
1 parent 6e86f97 commit 126c86a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ Installation

pip install flask-sock

Configuration
-------------

The only configuration option is ``SOCK_SERVER_OPTIONS``. If this option is
present in the application instance's ``config`` object, it must be a
dictionary with configuration options for the WebSocket server. Notable
options include:

- ``ping_interval``: Send ping packets to clients at the requested interval in
seconds. Set to ``None`` (the default) to disable ping/pong logic. Enable to
prevent disconnections when the line is idle for a certain amount of time, or
to detect unresponsive clients and disconnect them. A recommended interval is
25 seconds.
- ``max_message_size``: The maximum size allowed for a message, in bytes, or
``None`` for no limit. The default is ``None``.

Example::

app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 25}

The complete list of available options is documented in the constructor of the
`simple_websocket.Server <https://simple-websocket.readthedocs.io/en/latest/api.html#the-server-class>`_
class.

Example
-------

Expand Down
1 change: 1 addition & 0 deletions example/echo-eventlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask import Flask, render_template
from flask_sock import Sock
app = Flask(__name__)
app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 25}

sock = Sock(app)

Expand Down
1 change: 1 addition & 0 deletions example/echo-gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask import Flask, render_template
from flask_sock import Sock
app = Flask(__name__)
app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 25}

sock = Sock(app)

Expand Down
2 changes: 2 additions & 0 deletions example/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from flask_sock import Sock

app = Flask(__name__)
app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 25}

sock = Sock(app)


Expand Down
5 changes: 3 additions & 2 deletions src/flask_sock/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import wraps
from flask import Blueprint, request, Response
from flask import Blueprint, request, Response, current_app
from simple_websocket import Server, ConnectionClosed


Expand Down Expand Up @@ -53,7 +53,8 @@ def websocket_route(ws):
def decorator(f):
@wraps(f)
def websocket_route(*args, **kwargs): # pragma: no cover
ws = Server(request.environ)
ws = Server(request.environ, **current_app.config.get(
'SOCK_SERVER_OPTIONS', {}))
try:
f(ws, *args, **kwargs)
except ConnectionClosed:
Expand Down

1 comment on commit 126c86a

@lawndoc
Copy link

@lawndoc lawndoc commented on 126c86a Mar 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elegant solution, and thanks for the clear examples 😄

Please sign in to comment.