Websockets for Django with Centrifugo
Project description
Django Instant
Websockets for Django with Centrifugo.
- Push events into public or private channels.
- Handle the events in javascript client-side.
:sunny: Compatible: plug it on an existing Django instance without any modification in your main stack
Example
Push events in channels from anywhere in the code:
from instant.producers import publish
# Publish to a public channel
publish("public", "Message for everyone")
# Publish to a private channel with an event class set
publish("$users", "Message in logged in users channel", event_class="important")
# Publish to the staff channel with an extra json data payload
data = {"field1":"value1","field2":[1,2]}
publish("$staff", "Message for staff", data=data)
Quick start
Install the websockets server
Install the Centrifugo websockets server (see the detailled doc for more info). Download a release and generate a configuration file:
./centrifugo genconfig
The generated config.json
file looks like this:
{
"v3_use_offset": true,
"token_hmac_secret_key": "46b38493-147e-4e3f-86e0-dc5ec54f5133",
"admin_password": "ad0dff75-3131-4a02-8d64-9279b4f1c57b",
"admin_secret": "583bc4b7-0fa5-4c4a-8566-16d3ce4ad401",
"api_key": "aaaf202f-b5f8-4b34-bf88-f6c03a1ecda6",
"allowed_origins": []
}
Configure Django settings
Use these parameters to update your Django's settings.py
:
CENTRIFUGO_HOST = "http://localhost"
CENTRIFUGO_PORT = 8001
CENTRIFUGO_HMAC_KEY = "46b38493-147e-4e3f-86e0-dc5ec54f5133"
CENTRIFUGO_API_KEY = "aaaf202f-b5f8-4b34-bf88-f6c03a1ecda6"
SITE_NAME = "My site" # used in the messages to identify where they come from
Add "instant"
to INSTALLED_APPS
and update urls.py
:
urlpatterns = [
# ...
path("instant/", include("instant.urls")),
]
Create channels
Go into the admin to create channels
Avalailable endpoints
/instant/login/
: takes a username and password as parameter and will login the
user in Django and return a Centrifugo connection token
/instant/get_token/
: get a Centrifugo connection token for a logged in user
/instant/subscribe/
: get tokens for Centrifugo channels subscriptions
(doc)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_instant-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07f9b6375db882f18af12863bc27736319a9023d42f4230ce17972892501bff8 |
|
MD5 | 748ac041451c631fda27e369fa93c989 |
|
BLAKE2b-256 | 99b5b924f10e417645a47a5c2dcbb6c40eaaf77e33e39b54f1b194e2ea571123 |