Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin committed Nov 14, 2023
1 parent 3af4725 commit 4102d63
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
7 changes: 4 additions & 3 deletions docs/source/channels.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Django-Channels

```{warning}
This approach is not recommended anymore, please consider using [turbo_stream_from](./template-tags.md#turbo-stream-from) instead.
```

This library can also be used with [django-channels](https://channels.readthedocs.io/en/stable/). As with multiple streams, you can use the **TurboStream** class to broadcast turbo-stream content from your consumers.

```python
Expand Down Expand Up @@ -53,6 +57,3 @@ export default class extends Controller {
}
}
```

**Note** if you want to add reactivity directly to your models, so that model changes broadcast turbo-streams automatically, we recommend the [turbo-django](https://github.com/hotwire-django/turbo-django) package.

4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
# -- Project information -----------------------------------------------------

project = "django-turbo-response"
copyright = f"{datetime.datetime.now().year}, Dan Jacob"
author = "Dan Jacob"
copyright = f"{datetime.datetime.now().year}, Michael Yin"
author = "Michael Yin"


# -- General configuration ---------------------------------------------------
Expand Down
38 changes: 37 additions & 1 deletion docs/source/template-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,40 @@ This can help render `turbo-cable-stream-source` in Django template

`<turbo-cable-stream-source>` is a custom element provided by [turbo-rails](https://github.com/hotwired/turbo-rails/blob/097d8f90cf0c5ed24ac6b1a49cead73d49fa8ab5/app/javascript/turbo/cable_stream_source_element.js), with it, we can send Turbo Stream over the websocket connection and update the page in real time.

The `<turbo-cable-stream-source>` is built on Rails ActionCable, which provide many great feature out of the box, such as `Automatic Reconnection`, so we can focus on the business logic.
```{note}
1. To support Actioncable on the server, please install [django-actioncable](https://github.com/AccordBox/django-actioncable).
2. Please import `<turbo-cable-stream-source>` in your JavaScript code.
```

In `routing.py`, register `TurboStreamCableChannel`

```python
from actioncable import cable_channel_register
from turbo_response.cable_channel import TurboStreamCableChannel

cable_channel_register(TurboStreamCableChannel)
```

In Django template, we can subscribe to stream source like this:

```html
{% turbo_stream_from 'chat' view.kwargs.chat_pk %}
```

`turbo_stream_from` can accept multiple positional arguments

Then in Python code, we can send Turbo Stream to the stream source like this

```python
broadcast_render_to(
["chat", instance.chat_id],
template="message_append.turbo_stream.html",
context={
"instance": instance,
},
)
```

The `["chat", instance.chat_id]` **should** match the positional arguments in the `turbo_stream_from` tag.

The web page can be updated in real time, through Turbo Stream over Websocket.

0 comments on commit 4102d63

Please sign in to comment.