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
+18-13
Original file line number
Diff line number
Diff line change
@@ -340,16 +340,18 @@ This is often used to create chat systems, synchronize data between components,
340
340
341
341
| Name | Type | Description | Default |
342
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'` |
343
+
| `#!python name` | `#!python str | None` | The name of the channel to subscribe to. If you define a `#!python group_name`, you can keep `#!python name` undefined to auto-generate a unique name. | `#!python None` |
344
+
| `#!python group_name` | `#!python str | None` | If configured, any messages sent within this hook will be broadcasted to all channels in this group. | `#!python None` |
345
+
| `#!python group_add` | `#!python bool` | If `#!python True`, the channel will automatically be added to the group when the component mounts. | `#!python True` |
346
+
| `#!python group_discard` | `#!python bool` | If `#!python True`, the channel will automatically be removed from the group when the component dismounts. | `#!python True` |
347
+
| `#!python receiver` | `#!python AsyncMessageReceiver | None` | An async function that receives a `#!python message: dict` from a channel. If more than one receiver waits on the same channel name, a random receiver will get the result. | `#!python None` |
348
+
| `#!python layer` | `#!python str` | The channel layer to use. This layer must be defined in `#!python settings.py:CHANNEL_LAYERS`. | `#!python 'default'` |
347
349
348
350
<font size="4">**Returns**</font>
349
351
350
352
| Type | Description |
351
353
| --- | --- |
352
-
| `#!python AsyncMessageSender` | An async callable that can send a `#!python message: dict` |
354
+
| `#!python AsyncMessageSender` | An async callable that can send a `#!python message: dict`. |
353
355
354
356
??? warning "Extra Django configuration required"
355
357
@@ -359,13 +361,15 @@ This is often used to create chat systems, synchronize data between components,
359
361
360
362
In summary, you will need to:
361
363
362
-
1. Run the following command to install `channels-redis` in your Python environment.
364
+
1. Install [`redis`](https://redis.io/download/) on your machine.
365
+
366
+
2. Run the following command to install `channels-redis` in your Python environment.
363
367
364
368
```bash linenums="0"
365
369
pip install channels-redis
366
370
```
367
371
368
-
2. Configure your `settings.py` to use `RedisChannelLayer` as your layer backend.
372
+
3. Configure your `settings.py` to use `RedisChannelLayer` as your layer backend.
369
373
370
374
```python linenums="0"
371
375
CHANNEL_LAYERS = {
@@ -380,9 +384,9 @@ This is often used to create chat systems, synchronize data between components,
380
384
381
385
??? question "How do I broadcast a message to multiple components?"
382
386
383
-
By default, if more than one receiver waits on the same channel, a random one will get the result.
387
+
If more than one receiver waits on the same channel, a random one will get the result.
384
388
385
-
However, by defining `#!python group=True` you can configure a "groupchannel", which will broadcast messages to all receivers.
389
+
To get around this, you can define a `#!python group_name` to broadcast messages to all channels within a specific group. If you do not define a channel `#!python name` while using groups, ReactPy will automatically generate a unique channel name for you.
386
390
387
391
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
392
@@ -400,15 +404,16 @@ This is often used to create chat systems, synchronize data between components,
400
404
401
405
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
406
403
-
=== "components.py"
407
+
=== "signals.py"
404
408
405
409
```python
406
-
{% include "../../examples/python/use-channel-layer-signal-receiver.py" %}
410
+
{% include "../../examples/python/use-channel-layer-signal-sender.py" %}
407
411
```
408
-
=== "signals.py"
412
+
413
+
=== "components.py"
409
414
410
415
```python
411
-
{% include "../../examples/python/use-channel-layer-signal-sender.py" %}
416
+
{% include "../../examples/python/use-channel-layer-signal-receiver.py" %}
0 commit comments