Skip to content

Commit e1339e2

Browse files
committed
new docs
1 parent bfc730c commit e1339e2

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

docs/examples/python/use-channel-layer-group.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def my_receiver_component_1():
2323
async def receive_event(message):
2424
set_message(message["text"])
2525

26-
use_channel_layer(receiver=receive_event, group_name="my-group-name")
26+
use_channel_layer(group_name="my-group-name", receiver=receive_event)
2727

2828
return html.div(f"Message Receiver 1: {message}")
2929

@@ -35,6 +35,6 @@ def my_receiver_component_2():
3535
async def receive_event(message):
3636
set_message(message["text"])
3737

38-
use_channel_layer(receiver=receive_event, group_name="my-group-name")
38+
use_channel_layer(group_name="my-group-name", receiver=receive_event)
3939

4040
return html.div(f"Message Receiver 2: {message}")

docs/examples/python/use-channel-layer-signal-sender.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from django.dispatch import receiver
66

77

8-
class ExampleModel(Model):
9-
...
8+
class ExampleModel(Model): ...
109

1110

1211
@receiver(pre_save, sender=ExampleModel)
@@ -17,4 +16,4 @@ def my_sender_signal(sender, instance, **kwargs):
1716
async_to_sync(layer.send)("my-channel-name", {"text": "Hello World!"})
1817

1918
# Example of sending a message to a group channel
20-
async_to_sync(layer.group_send)("my-channel-name", {"text": "Hello World!"})
19+
async_to_sync(layer.group_send)("my-group-name", {"text": "Hello World!"})

docs/src/reference/hooks.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,18 @@ This is often used to create chat systems, synchronize data between components,
340340

341341
| Name | Type | Description | Default |
342342
| --- | --- | --- | --- |
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'` |
347349

348350
<font size="4">**Returns**</font>
349351

350352
| Type | Description |
351353
| --- | --- |
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`. |
353355

354356
??? warning "Extra Django configuration required"
355357

@@ -359,13 +361,15 @@ This is often used to create chat systems, synchronize data between components,
359361

360362
In summary, you will need to:
361363

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.
363367

364368
```bash linenums="0"
365369
pip install channels-redis
366370
```
367371

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.
369373

370374
```python linenums="0"
371375
CHANNEL_LAYERS = {
@@ -380,9 +384,9 @@ This is often used to create chat systems, synchronize data between components,
380384

381385
??? question "How do I broadcast a message to multiple components?"
382386

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.
384388

385-
However, by defining `#!python group=True` you can configure a "group channel", 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.
386390

387391
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).
388392

@@ -400,15 +404,16 @@ This is often used to create chat systems, synchronize data between components,
400404

401405
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.
402406

403-
=== "components.py"
407+
=== "signals.py"
404408

405409
```python
406-
{% include "../../examples/python/use-channel-layer-signal-receiver.py" %}
410+
{% include "../../examples/python/use-channel-layer-signal-sender.py" %}
407411
```
408-
=== "signals.py"
412+
413+
=== "components.py"
409414

410415
```python
411-
{% include "../../examples/python/use-channel-layer-signal-sender.py" %}
416+
{% include "../../examples/python/use-channel-layer-signal-receiver.py" %}
412417
```
413418

414419
---

0 commit comments

Comments
 (0)