You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Run with docker `docker compose run binder ./setup.py test` (but you may need to
15
15
16
16
The tests are set up in such a way that there is no need to keep migration files. The setup procedure in `tests/__init__.py` handles the preparation of the database by directly calling some build-in Django commands.
17
17
18
-
To only run a selection of the tests, use the `-s` flag like `./setup.py test -s tests.test_some_specific_test`.
18
+
To only run a selection of the tests, use the `-s` flag like `docker compose run binder ./setup.py test -s tests.test_some_specific_test`.
Copy file name to clipboardExpand all lines: docs/websockets.md
+25-5Lines changed: 25 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@ Binder.websockets contains functions to connect with a [high-templar](https://gi
4
4
5
5
## Flow
6
6
7
-
The client = a web/native/whatever frontend
8
-
The websocket server = a high-templar instance
7
+
The client = a web/native/whatever frontend
8
+
The websocket server = a high-templar instance
9
9
The binder app = a server created with django-binder
10
10
11
11
- The client needs live updates of certain models.
@@ -24,7 +24,7 @@ The scoping of subscriptions is done through rooms. Roomnames are dictionaries.
24
24
25
25
There is a chat application for a company. A manager can only view messages of a single location.
26
26
27
-
The allowed_rooms of a manager of the eindhoven branch could look like
27
+
The allowed_rooms of a manager of the eindhoven branch could look like
28
28
```
29
29
[{'location': 'Eindhoven'}]
30
30
```
@@ -43,9 +43,30 @@ Note: this doesn't mean a client can subscribe to room: `{'location': '*'}` and
43
43
44
44
If you do really need a room with messages from all locations, just trigger twice: once in the location specific room and one in the location: * room.
45
45
46
+
## Trigger on saves
47
+
Since sending websocket updates upon saving models is something we often need, there is a 'shortcut' for this.
48
+
If you set `push_websocket_updates_upon_save` to `True` in a model, it will automatically send websocket updates whenever it is saved or deleted.
49
+
50
+
```python
51
+
classCountry(BinderModel):
52
+
push_websocket_updates_upon_save =True
53
+
name = models.CharField(unique=True, max_length=100)
54
+
```
55
+
For instance, whenever a `Country` is saved, it will trigger a websocket update to `auto-updates/country` with `data = country.id`.
56
+
57
+
### Custom object managers
58
+
Normally, websocket updates are also sent when an object is bulk created/updated/deleted. This is implemented by using a custom objects `Manager`.
59
+
This is usually just an implementation detail, but it can be problematic when your model *also* has its own custom objects `Manager`.
60
+
If you want to make bulk updating push websocket notifications, you need to ensure that your custom manager inherits from `binder.models.BinderManager`.
61
+
62
+
### Forcing websocket updates
63
+
If you want stores to re-fetch your objects, but you haven't saved them directly (e.g. when you changed related objects or annotation values),
64
+
you can forcibly send a websocket update by calling the `push_default_websocket_update()` method on an instance of your model.
65
+
66
+
46
67
## Binder setup
47
68
48
-
The high-templar instance is agnostic of the authentication/datamodel/permissions. The authentication is done by the proxy to /api/bootstrap. The datamodel / permission stuff is all done through rooms and the data that gets sent through it.
69
+
The high-templar instance is agnostic of the authentication/datamodel/permissions. The authentication is done by the proxy to /api/bootstrap. The datamodel / permission stuff is all done through rooms and the data that gets sent through it.
49
70
50
71
`binder.websocket` provides 2 helpers for communicating with high-templar.
51
72
@@ -74,4 +95,3 @@ The RoomController checks every descendant of the ModelView and looks for a `@c
74
95
### Trigger
75
96
76
97
`binder.websocket` provides a `trigger` to the high_templar instance using a POST request. The url for this request is `getattr(settings, 'HIGH_TEMPLAR_URL', 'http://localhost:8002')`. It needs `data, rooms` as args, the data which will be sent in the publish and the rooms it will be publishes to.
0 commit comments