Skip to content

Commit 7990ac3

Browse files
committed
settings: Make the websocket settings clearer.
It is now using `WEBSOCKET_BACK_HOST`, `WEBSOCKET_BACK_PORT` and `WEBSOCKET_FRONT_URI`. We need to take in consideration that the "front" WebSocket address (that clients will connect to) might be different than the "back" ip and port which are bound in the host. This happens for instance for reverse proxies, or when running inside a container. We considered using a `WEBSOCKET_TLS` setting, to try guessing the "front" address based on `WEBSOCKET_HOST`, `WEBSOCKET_PORT` and `WEBSOCKET_TLS`, but as the back and front address can differ, this would need to introduce a `WEBSOCKET_URI` in any case, so we went with just using it, and not adding an extra `WEBSOCKET_TLS`.
1 parent d4db078 commit 7990ac3

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

docs/config/settings.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,15 @@ Otherwise, use any valid [python-social-auth configuration](https://python-socia
271271

272272
#### WEBSOCKET_ENABLED
273273

274-
A websocket server is packaged with uMap, and can be turned-on to activate "real-time collaboration".
275-
In practice, you would need to run the websocket server and specify a set of settings.
274+
A WebSocket server is packaged with uMap, and can be turned-on to activate
275+
"real-time collaboration". In practice, in order to enable it, a few settings
276+
are exposed.
276277

277-
Turning this setting to `True` **will make a switch available** on each map, to enable "real-time collaboration".
278+
Setting `WEBSOCKET_ENABLED` to `True` will **not** enable real-time
279+
collaboration on all the maps served by the server. Instead, a switch will be
280+
available in the "advanced properties" of the map.
278281

279-
You can run the websocket server with this command:
282+
The websocket server can be run with the following command:
280283

281284
```bash
282285
python -m umap.ws
@@ -286,19 +289,31 @@ Configuration example:
286289

287290
```python
288291
WEBSOCKET_ENABLED = True
289-
WEBSOCKET_HOST = "localhost"
290-
WEBSOCKET_PORT = 8002
291-
WEBSOCKET_URI = "ws://localhost:8002"
292+
WEBSOCKET_BACK_HOST = "localhost"
293+
WEBSOCKET_BACK_PORT = 8002
294+
WEBSOCKET_FRONT_URI = "ws://localhost:8002"
292295
```
293296

294297
These settings can also be set with the (same names) environment variables.
295298

296-
#### WEBSOCKET_HOST
297-
#### WEBSOCKET_PORT
299+
#### WEBSOCKET_BACK_HOST
300+
#### WEBSOCKET_BACK_PORT
298301

299-
The host and port for the websocket server.
302+
The internal host and port the websocket server will connect to.
300303

301-
#### WEBSOCKET_URI
304+
#### WEBSOCKET_FRONT_URI
302305

303-
The connection string that will be used by the client to connect to the websocket server.
304-
Use `wss://host:port` if the server is behind TLS, and `ws://host:port` otherwise.
306+
The connection string that will be used by the client to connect to the
307+
websocket server. In practice, as it's useful to put the WebSocket server behind
308+
TLS encryption, the values defined by `WEBSOCKET_FRONT_URI` are different than
309+
the values defined by `WEBSOCKET_BACK_PORT` and `WEBSOCKET_BACK_HOST`.
310+
311+
This value is comprised of three parts:
312+
313+
```
314+
protocol://host:port
315+
```
316+
317+
- `protocol`: can either be `ws` for plain unencrypted WebSockets, or `wss` when using TLS encryption.
318+
- `host`: is the address where the connection will be sent. It should be public facing.
319+
- `port`: is the port that is open on the host.

umap/settings/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Base settings shared by all environments"""
22

33
# Import global settings to make it easier to extend settings.
4+
import os
45
from email.utils import parseaddr
56

67
import environ
78
from django.conf.locale import LANG_INFO
89

10+
import umap as project_module
11+
912
env = environ.Env()
1013

1114
# =============================================================================
@@ -137,9 +140,6 @@
137140
# Calculation of directories relative to the project module location
138141
# =============================================================================
139142

140-
import os
141-
142-
import umap as project_module
143143

144144
PROJECT_DIR = os.path.dirname(os.path.realpath(project_module.__file__))
145145

@@ -310,6 +310,6 @@
310310
# WebSocket configuration
311311

312312
WEBSOCKET_ENABLED = env.bool("WEBSOCKET_ENABLED", default=False)
313-
WEBSOCKET_HOST = env("WEBSOCKET_HOST", default="localhost")
314-
WEBSOCKET_PORT = env.int("WEBSOCKET_PORT", default=8001)
315-
WEBSOCKET_URI = env("WEBSOCKET_URI", default="ws://localhost:8001")
313+
WEBSOCKET_BACK_HOST = env("WEBSOCKET_BACK_HOST", default="localhost")
314+
WEBSOCKET_BACK_PORT = env.int("WEBSOCKET_BACK_PORT", default=8001)
315+
WEBSOCKET_FRONT_URI = env("WEBSOCKET_FRONT_URI", default="ws://localhost:8001")

umap/tests/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727

2828

2929
WEBSOCKET_ENABLED = True
30-
WEBSOCKET_PORT = "8010"
31-
WEBSOCKET_URI = "ws://localhost:8010"
30+
WEBSOCKET_BACK_PORT = "8010"
31+
WEBSOCKET_FRONT_URI = "ws://localhost:8010"

umap/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def get_map_properties(self):
504504
"umap_version": VERSION,
505505
"featuresHaveOwner": settings.UMAP_DEFAULT_FEATURES_HAVE_OWNERS,
506506
"websocketEnabled": settings.WEBSOCKET_ENABLED,
507-
"websocketURI": settings.WEBSOCKET_URI,
507+
"websocketURI": settings.WEBSOCKET_FRONT_URI,
508508
}
509509
created = bool(getattr(self, "object", None))
510510
if (created and self.object.owner) or (not created and not user.is_anonymous):

umap/ws.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,20 @@ async def handler(websocket):
8383

8484
async def main():
8585
if not settings.WEBSOCKET_ENABLED:
86-
print("WEBSOCKET_ENABLED should be set to True to run the WebSocket Server")
86+
msg = (
87+
"WEBSOCKET_ENABLED should be set to True to run the WebSocket Server. "
88+
"See the documentation at "
89+
"https://docs.umap-project.org/en/stable/config/settings/#websocket_enabled "
90+
"for more information."
91+
)
92+
print(msg)
8793
exit(1)
8894

89-
async with serve(handler, settings.WEBSOCKET_HOST, settings.WEBSOCKET_PORT):
90-
print(
91-
(
92-
f"Waiting for connections on {settings.WEBSOCKET_HOST}:{settings.WEBSOCKET_PORT}"
93-
)
94-
)
95+
host = settings.WEBSOCKET_BACK_HOST
96+
port = settings.WEBSOCKET_BACK_PORT
97+
98+
async with serve(handler, host, port):
99+
print(f"Waiting for connections on {host}:{port}")
95100
await asyncio.Future() # run forever
96101

97102

0 commit comments

Comments
 (0)