|
3 | 3 | Live updating |
4 | 4 | ============= |
5 | 5 |
|
6 | | -Live updating blah. |
| 6 | +Live updating is supported using additional ``Dash`` :ref:`components <dash_components>` and |
| 7 | +leveraging `Django Channels <https://channels.readthedocs.io/en/latest/>`_ to provide websocket endpoints. |
| 8 | + |
| 9 | +Server-initiated messages are sent to all interested clients. The content of the message is then injected into |
| 10 | +the application from the client, and from that point it is handled like any other value passed to a callback function. |
| 11 | +The messages are constrained to be JSON serialisable, as that is how they are transmitted to and from the clients, and should |
| 12 | +also be as small as possible given that they travel from the server, to each interested client, and then back to the |
| 13 | +server again as an argument to one or more callback functions. |
| 14 | + |
| 15 | +Live updating requires a server setup that is considerably more |
| 16 | +complex than the alternative, namely use of the built-in `Interval <https://dash.plot.ly/live-updates>`_ component. However, live |
| 17 | +updating can be used to reduce server load (as callbacks are only made when needed) and application latency (as callbacks are |
| 18 | +invoked as needed, not on the tempo of the Interval component). |
7 | 19 |
|
8 | 20 | Message channels |
9 | 21 | ---------------- |
10 | 22 |
|
11 | 23 | Messages are passed through named channels, and each message consists |
12 | | -of a ``label`` and ``value`` pair. |
| 24 | +of a ``label`` and ``value`` pair. A :ref:`Pipe <pipe_component>` component is provided that listens for messages and makes |
| 25 | +them available to ``Dash`` callbacks. Each message is sent through a message channel to all ``Pipe`` components that have |
| 26 | +registered their interest in that channel, and in turn the components will select messages by ``label``. |
| 27 | + |
| 28 | +A message channel exists as soon as a component signals that it is listening for messages on it. The |
| 29 | +message delivery requirement is 'hopefully at least once'. In other words, applications should be robust against both the failure |
| 30 | +of a message to be delivered, and also for a message to be delivered multiple times. A design approach that has messages |
| 31 | +of the form 'you should look at X and see if something should be done' is strongly encouraged. The accompanying demo has |
| 32 | +messages of the form 'button X at time T', for example. |
| 33 | + |
| 34 | +Sending messages from within Django |
| 35 | +----------------------------------- |
| 36 | + |
| 37 | +Messages can be easily sent from within Django, provided that they are within the ASGI server. |
13 | 38 |
|
14 | | -Pipes |
15 | | ------ |
| 39 | +.. code-block:: python |
16 | 40 |
|
17 | | -A :ref:`Pipe <pipe_component>` component is provided. |
| 41 | + from django_plotly_dash.consumers import send_to_pipe_channel |
| 42 | +
|
| 43 | + # Send a message |
| 44 | + # |
| 45 | + # This function may return *before* the message has been sent |
| 46 | + # to the pipe channel. |
| 47 | + # |
| 48 | + send_to_pipe_channel(channel_name="live_button_counter", |
| 49 | + label="named_counts", |
| 50 | + value=value) |
| 51 | +
|
| 52 | + # Send a message asynchronously |
| 53 | + # |
| 54 | + await async_send_to_pipe_channel(channel_name="live_button_counter", |
| 55 | + label="named_counts", |
| 56 | + value=value) |
| 57 | +
|
| 58 | +In general, making assumptions about the ordering of code between message sending and receiving is |
| 59 | +unsafe. The ``send_to_pipe`` function uses the Django Channels ``async_to_sync`` wrapper around |
| 60 | +a call to ``async_send_to_pipe`` and therefore may return before the asynchronous call is made (perhaps |
| 61 | +on a different thread). Furthermore, the transit of the message through the channels backend |
| 62 | +introduces another indeterminacy. |
18 | 63 |
|
19 | 64 | HTTP Endpoint |
20 | 65 | ------------- |
21 | 66 |
|
22 | 67 | There is an HTTP endpoint that allows direct insertion of messages into a message channel. |
23 | 68 |
|
| 69 | +Deployment |
| 70 | +---------- |
| 71 | + |
| 72 | +Need redis and daphne. |
| 73 | + |
0 commit comments