Skip to main content

Getting started with Django

Markdown

Live updates, chat, who is online, calls, group calls and live streams in a Django app, through WordPlus Cloud. Two packages:

  • Your server (wordplus-realtime, with wordplus_realtime.django) declares your rooms once, says who may join them and call whom, and sends events to rooms and to users: the browser routes for the session user, the page settings with the CSRF token, and profiles from the Django user model.
  • The page (@wordplus/realtime, the client your routes serve it, with no Node) connects to WordPlus Cloud, joins rooms, and sends and gets events: emit, emitWithAck, volatile.

What goes through WordPlus Cloud is sealed with keys only your server and the browsers of your users hold. Ready-made parts come with it, all optional: the Better Messages chat, its list of who is online and its call screen, and the Easy Stream player. Without them, your own components draw everything on the same API (your own interface). The example app is a whole shop built this way.

Your framework can run websockets of its own. Why WordPlus Cloud says what the cloud runs for you, and what it adds.

Try it live at django.docs.wordplus.cloud: the example app of this guide as a demo, a shop of your own for a day, with Ben and Cara opening in windows of their own.

Two browsers side by side: Ann marks order 1042 as shipped on the staff page of the shop, and the order page of Ben shows Shipped at once, without a reload

1. Your app in WordPlus Cloud​

Apps come with a subscription. Choose your connections, starting from 1,000, and your WordPlus Cloud account gets its Apps page.

In your WordPlus Cloud account, go to Apps → Add app. Name it, and list the addresses its pages are served from: https://shop.example.com, and http://localhost:8000 while you develop. Only pages on those addresses may connect.

You get the app key, public because every page carries it, and its secret, for your server only. Make a local key of your own too: everything sealed is sealed with keys derived from it, and it never leaves your servers.

WORDPLUS_SITE=s1500000007
WORDPLUS_SECRET=…
WORDPLUS_LOCAL_KEY=… # openssl rand -hex 32; the same on every server of the app, kept as long as the app

Django reads no .env by itself: set them in the environment of the server process, or read the file at the top of settings.py, such as with python-dotenv (load_dotenv(BASE_DIR / ".env")). The package takes them as Django starts, and checks them when first used, so manage.py migrate and collectstatic run without them.

2. Install​

pip install 'wordplus-realtime[django]'     # uv add works the same

That is all, with no Node: the package carries the browser files, and its routes serve them. The client as an ES module, the worker that lets the tabs of a user share one connection, the room client for group calls, and realtime-ui.min.js and realtime-ui-streams.min.js, the ready-made parts, the second for live streams. Each release serves the ones its package came with, from a folder named after their version.

# settings.py
INSTALLED_APPS = [
# …
"wordplus_realtime.django", # its template tag, {% wordplus_realtime %}
]

3. Your server​

Each room is named after what it carries: order:1042 for the updates of order 1042. A page that joins it asks your server first, and a rule answers: a function your server declares once for every room whose name fits the pattern order:{id}. WordPlus Cloud then holds the page to the answer (permissions). This rule says:

  1. Visitors who are not signed in stay out.
  2. The customer of the order joins, and nobody else.

And the room keeps its last 20 updates, for a page that reconnects.

# shop/realtime.py: the app's Realtime, made from WORDPLUS_SITE, WORDPLUS_SECRET and WORDPLUS_LOCAL_KEY
from wordplus_realtime.django import realtime

from .models import Order

# The room of an order, order:1042, the last 20 updates kept.
def may_follow_order(user, p):
# 1. Visitors stay out.
if user is None:
return False

# 2. The customer of the order, and nobody else.
return Order.objects.filter(pk=p["id"], customer_id=user).exists()

realtime.room("order:{id}", may_follow_order, history=20)
# shop/apps.py: the rooms are declared as Django starts, before the first request
from django.apps import AppConfig

class ShopConfig(AppConfig):
name = "shop"

def ready(self):
from . import realtime # noqa: F401
# urls.py: POST /api/realtime/auth, /calls, /keys and /webhook, and GET /api/realtime/files/…, the browser files
from django.urls import include, path

urlpatterns = [
path("api/realtime/", include("wordplus_realtime.django.routes")),
# …
]
  • The user is the session (request.user), and the Django CSRF check stays on for the browser routes. The webhook is exempt, as the realtime server signs it, and the browser files ask no session.
  • What others see of a user is their full name, or their username, from your user model.
  • Its settings, all optional, go in WORDPLUS_REALTIME by name: {"base": "/api/realtime", "user": "shop.auth.current_user", "profile": "shop.people.profile_of"}, functions by their dotted path.
  • The pattern of a room names its parts: order:{id} matches order:1042, and the rule gets {"id": "1042"}.
  • The rules are plain functions that may query your models the usual way.

4. The page​

{# shop/templates/shop/order.html #}
{% load static wordplus_realtime %}
<head>
{% wordplus_realtime %}
<script type="module" src="{% static 'order.js' %}"></script>
</head>
// shop/static/order.js, as it is: no Node and no bundler
import { connect } from '@wordplus/realtime';

const rt = await connect( window.wordplusRealtimeConfig );

const order = rt.join( 'order:1042' );
order.on( 'updated', ( data ) => showStatus( data.status ) );
order.on( 'error', ( e ) => console.warn( e.code ) ); // forbidden, no_rule …

{% wordplus_realtime %} writes the settings for the request user, the routes with the CSRF token as X-CSRFToken and the user keys, so the page connects with no request first, and an import map naming @wordplus/realtime the client the routes serve, so order.js imports it as it is.

  • It needs the request in the template context: django.template.context_processors.request, which new projects have.
  • A view that knows its user another way names them: {% wordplus_realtime user=customer.pk %}.
  • With a bundler of your own, npm install @wordplus/realtime, import it the same way, and leave the map out: {% wordplus_realtime importmap=False %}.
  • A Realtime of your own, made with Realtime.from_environment(…), takes path("api/realtime/", include(wr.urls(realtime))) with import wordplus_realtime.django as wr, and the tag takes that one.

5. Send​

After your own work is saved, once the transaction commits:

from django.db import transaction

order.status = "shipped"
order.save()
transaction.on_commit(lambda: realtime.to(f"order:{order.pk}").emit("updated", {"status": order.status}))

Everyone in the room gets { status }, sealed with the room key on the way, and a page that reconnects later gets it from the room history.

Next​

What to build next​

Each use case is a page of its own, with the rules of your server and the code of the page: chat, who is online, live updates and notifications, calls from a button, team meetings, live streaming and the rest in the use cases. Three of them build a small Node.js app from an empty folder, every file in full.