Quickstart

Want to see Turbo in action? Here’s a simple broadcast that can be setup in less than a minute.

The basics:

  • A Turbo Stream class is declared in python.

  • A template subscribes to the Turbo Stream.

  • HTML is be pushed to all subscribed pages which replaces the content of specified HTML p tag.

Example

First, declare the Stream.

streams.py
import turbo

class BroadcastStream(turbo.Stream):
    pass

Then, create a template that subscribes to the stream.

urls.py
from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('', TemplateView.as_view(template_name='broadcast_example.html'))
]
broadcast_example.html
{% load turbo_streams %}
<!DOCTYPE html>
<html lang="en">
<head>
    {% include "turbo/head.html" %}
</head>
<body>
    {% turbo_subscribe 'quickstart:BroadcastStream' %}

    <p class="broadcast_box_class" id="broadcast_box">Placeholder for broadcast</p>
</body>
</html>

Note

Broadcasts can target any HTML element on a page subscribed to its stream. Target elements do not need be wrapped in any turbo style tag.

Now open ./manage.py shell. Import the Turbo Stream and tell the stream to take the current timestamp and update the element with id broadcast_box on all subscribed pages.

from quickstart.streams import BroadcastStream
from datetime import datetime

BroadcastStream().update(text=f"{datetime.now()}: This is a broadcast.", id="broadcast_box")

With the quickstart/ path open in a browser window, watch as the broadcast pushes messages to the page.

Now change .update() to .append() and resend the broadcast a few times. Notice you do not have to reload the page to get this modified behavior.

Excited to learn more? Be sure to walk through the tutorial and read more about what the Turbo class can do.