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: docs/src/reference/hooks.md
+102-1Lines changed: 102 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -275,7 +275,11 @@ Mutation functions can be sync or async.
275
275
276
276
### Use User Data
277
277
278
-
Store or retrieve data (`#!python dict`) specific to the connection's `#!python User`. This data is stored in the `#!python REACTPY_DATABASE`.
278
+
Store or retrieve a `#!python dict` containing user data specific to the connection's `#!python User`.
279
+
280
+
This hook is useful for storing user-specific data, such as preferences, settings, or any generic key-value pairs.
281
+
282
+
User data saved with this hook is stored within the `#!python REACTPY_DATABASE`.
279
283
280
284
=== "components.py"
281
285
@@ -312,6 +316,103 @@ Store or retrieve data (`#!python dict`) specific to the connection's `#!python
312
316
313
317
---
314
318
319
+
## Communication Hooks
320
+
321
+
---
322
+
323
+
### Use Channel Layer
324
+
325
+
Subscribe to a [Django Channels layer](https://channels.readthedocs.io/en/latest/topics/channel_layers.html) to send/receive messages.
326
+
327
+
Layers are a multiprocessing-safe communication system that allows you to send/receive messages between different parts of your application.
328
+
329
+
This is often used to create chat systems, synchronize data between components, or signal re-renders from outside your components.
330
+
331
+
=== "components.py"
332
+
333
+
```python
334
+
{% include "../../examples/python/use-channel-layer.py" %}
335
+
```
336
+
337
+
??? example "See Interface"
338
+
339
+
<font size="4">**Parameters**</font>
340
+
341
+
| Name | Type | Description | Default |
342
+
| --- | --- | --- | --- |
343
+
| `#!python name` | `#!python str` | The name of the channel to subscribe to. | N/A |
344
+
| `#!python receiver` | `#!python AsyncMessageReceiver | None` | An async function that receives a `#!python message: dict` from the channel layer. If more than one receiver waits on the same channel, a random one will get the result (unless `#!python group=True` is defined). | `#!python None` |
345
+
| `#!python group` | `#!python bool` | If `#!python True`, a "group channel" will be used. Messages sent within a group are broadcasted to all receivers on that channel. | `#!python False` |
346
+
| `#!python layer` | `#!python str` | The channel layer to use. These layers must be defined in `#!python settings.py:CHANNEL_LAYERS`. | `#!python 'default'` |
347
+
348
+
<font size="4">**Returns**</font>
349
+
350
+
| Type | Description |
351
+
| --- | --- |
352
+
| `#!python AsyncMessageSender` | An async callable that can send a `#!python message: dict` |
353
+
354
+
??? warning "Extra Django configuration required"
355
+
356
+
In order to use this hook, you will need to configure Django to enable channel layers.
357
+
358
+
The [Django Channels documentation](https://channels.readthedocs.io/en/latest/topics/channel_layers.html#configuration) has information on what steps you need to take.
359
+
360
+
In summary, you will need to:
361
+
362
+
1. Run the following command to install `channels-redis` in your Python environment.
363
+
364
+
```bash linenums="0"
365
+
pip install channels-redis
366
+
```
367
+
368
+
2. Configure your `settings.py` to use `RedisChannelLayer` as your layer backend.
??? question "How do I broadcast a message to multiple components?"
382
+
383
+
By default, if more than one receiver waits on the same channel, a random one will get the result.
384
+
385
+
However, by defining `#!python group=True` you can configure a "group channel", which will broadcast messages to all receivers.
386
+
387
+
In the example below, all messages sent by the `#!python sender` component will be received by all `#!python receiver` components that exist (across every active client browser).
388
+
389
+
=== "components.py"
390
+
391
+
```python
392
+
{% include "../../examples/python/use-channel-layer-group.py" %}
393
+
```
394
+
395
+
??? question "How do I signal a re-render from something that isn't a component?"
396
+
397
+
There are occasions where you may want to signal a re-render from something that isn't a component, such as a Django model signal.
398
+
399
+
In these cases, you can use the `#!python use_channel_layer` hook to receive a signal within your component, and then use the `#!python get_channel_layer().send(...)` to send the signal.
400
+
401
+
In the example below, the sender will send a signal every time `#!python ExampleModel` is saved. Then, when the receiver component gets this signal, it explicitly calls `#!python set_message(...)` to trigger a re-render.
402
+
403
+
=== "components.py"
404
+
405
+
```python
406
+
{% include "../../examples/python/use-channel-layer-signal-receiver.py" %}
407
+
```
408
+
=== "signals.py"
409
+
410
+
```python
411
+
{% include "../../examples/python/use-channel-layer-signal-sender.py" %}
Copy file name to clipboardExpand all lines: docs/src/reference/router.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
@@ -2,7 +2,7 @@
2
2
3
3
<pclass="intro"markdown>
4
4
5
-
A variant of [`reactpy-router`](https://github.com/reactive-python/reactpy-router) that utilizes Django conventions.
5
+
A Single Page Application URL router, which is a variant of [`reactpy-router`](https://github.com/reactive-python/reactpy-router) that uses Django conventions.
0 commit comments