All Projects → pennlabs → django-rest-live

pennlabs / django-rest-live

Licence: MIT license
Subscribe to updates from Django REST Framework over Websockets.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to django-rest-live

Codeigniter Ratchet Websocket
This library contains the demo of commenting/posting realtime using CodeIgniter+AngularJS+Ratchet PHP Websocket
Stars: ✭ 84 (+75%)
Mutual labels:  websockets, realtime
Codenames
Stars: ✭ 159 (+231.25%)
Mutual labels:  websockets, realtime
Sandstone
PHP microframework designed to build a RestApi working together with a websocket server. Build a real time RestApi!
Stars: ✭ 98 (+104.17%)
Mutual labels:  websockets, realtime
Channelslightscontrol
Demo app with Django Channels to control Lights over websockets. Made for PyStPete meetup(https://www.meetup.com/Saint-Petersburg-Python-Meetup/).
Stars: ✭ 14 (-70.83%)
Mutual labels:  websockets, realtime
docs
The official soketi documentation. 📡
Stars: ✭ 55 (+14.58%)
Mutual labels:  websockets, realtime
Django Channels React Multiplayer
turn based strategy game using django channels, redux, and react hooks
Stars: ✭ 52 (+8.33%)
Mutual labels:  websockets, django-rest-framework
Next Todos
200 lines realtime todos app powered by next.js, preact, jet, redux and now
Stars: ✭ 117 (+143.75%)
Mutual labels:  websockets, realtime
Wsify
Just a tiny, simple and real-time self-hosted pub/sub messaging service
Stars: ✭ 452 (+841.67%)
Mutual labels:  websockets, realtime
channeled-dashboard
Repository for the talk `Building real time applications with Django and Channels`
Stars: ✭ 20 (-58.33%)
Mutual labels:  websockets, realtime
Socketcluster Client
JavaScript client for SocketCluster
Stars: ✭ 250 (+420.83%)
Mutual labels:  websockets, realtime
Stl.fusion
Get real-time UI updates in Blazor apps and 10-1000x faster API responses with a novel approach to distributed reactive computing. Fusion brings computed observables and automatic dependency tracking from Knockout.js/MobX/Vue to the next level by enabling a single dependency graph span multiple servers and clients, including Blazor apps running in browser.
Stars: ✭ 858 (+1687.5%)
Mutual labels:  websockets, realtime
intrinio-realtime-python-sdk
Intrinio Python SDK for Real-Time Stock Prices
Stars: ✭ 79 (+64.58%)
Mutual labels:  websockets, realtime
Supabase
The open source Firebase alternative. Follow to stay updated about our public Beta.
Stars: ✭ 25,142 (+52279.17%)
Mutual labels:  websockets, realtime
Socketcluster Server
Minimal server module for SocketCluster
Stars: ✭ 70 (+45.83%)
Mutual labels:  websockets, realtime
Vue Socket.io Extended
✌️⚡️ Socket.io bindings for Vue.js and Vuex (inspired by Vue-Socket.io)
Stars: ✭ 506 (+954.17%)
Mutual labels:  websockets, realtime
Entangled
Rails in real time
Stars: ✭ 108 (+125%)
Mutual labels:  websockets, realtime
Angelo
Sinatra-like DSL for Reel that supports WebSockets and SSE
Stars: ✭ 303 (+531.25%)
Mutual labels:  websockets, realtime
Sapphiredb
SapphireDb Server, a self-hosted, easy to use realtime database for Asp.Net Core and EF Core
Stars: ✭ 326 (+579.17%)
Mutual labels:  websockets, realtime
Node Jet
Realtime Message Bus for the Web. Javascript Implementation
Stars: ✭ 162 (+237.5%)
Mutual labels:  websockets, realtime
soketi
Just another simple, fast, and resilient open-source WebSockets server. 📣
Stars: ✭ 2,202 (+4487.5%)
Mutual labels:  websockets, realtime

Django REST Live

Documentation CircleCI Coverage Status PyPi Package

Django REST Live enables clients which use an API built with Django REST Framework to receive a stream of updates for querysets and model instances over a websocket connection managed by Django Channels. There had been plans for real-time websocket support in REST Framework on a few occasions (2016, 2018), but at the time, async support in Django was in the early planning stages and Channels was being rewritten with breaking API changes.

This plugin aims to bridge that gap between Channels and REST Framework while being as generic and boilerplate-free as possible. Clients are be able to subscribe to real-time updates for any queryset that's exposed through a Generic API View or any of its subclasses, including Model ViewSet, with just one mixin!

Check out the full tutorial and reference documentation for specifics.

Dependencies

Installation and Setup

Make sure to install and properly set up Django Channels before installing django-rest-live.

pip install django-rest-live

Add rest_live to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "rest_framework",
    "channels",
    "rest_live",
]

Create a RealtimeRouter in your ASGI routing file (generally asgi.py) and add the router's consumer to the websocket routing you set up with Django Channels. Feel free to choose any URL endpoint for the websocket, here we've chosen /ws/subscribe/.

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from django.core.asgi import get_asgi_application
from rest_live.routers import RealtimeRouter

router = RealtimeRouter()

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
    URLRouter([
        path("ws/subscribe/", router.as_consumer().as_asgi(), name="subscriptions"),
    ])
),
})

Configuration

Check out the Tutorial for an in-depth example.

To allow subscriptions to a queryset, add the RealtimeMixin to a GenericAPIView or ModelViewSet that exposes that queryset. Then, register the view with the RealtimeRouter instance you created during setup.

...
router = RealtimeRouter()
router.register(MyViewSet)  # Register all ViewSets here
...

Client-Side

Subscribing to a updates equires opening a WebSocket on the client connection to the URL you specified during setup. Feel free to use any frontend web framework you'd like. Below is a simple example in vanilla JavaScript which logs updates to the console.

const socket = new WebSocket("ws://<django_domain_here>/ws/subscribe");

socket.addEventListener("message", function (event) {
  console.log("Update received:", JSON.parse(event.data));
});

// Subscribe to updates for the model instance with the ID of 1.
socket.send(
  JSON.stringify({
    id: 1337,
    type: "subscribe",
    model: "appname.ModelName",
    action: "retrieve",
    lookup_by: 1,
  })
);

// Subscribe to updates for every model in the queryset.
socket.send(
  JSON.stringify({
    id: 1338,
    type: "subscribe",
    model: "appname.ModelName",
    action: "list",
  })
);

// After 5 seconds, unsubscribe from updates for the single model instance with ID 1.
setTimeout(5 * 1000, () =>
  socket.sent(
    JSON.stringify({
      type: "unsubscribe",
      id: 1337,
    })
  )
);

Broadcast updates will be sent from the server in this format:

{
  "type": "broadcast",
  "id": 1337,
  "model": "appname.ModelName",
  "action": "UPDATED",
  "instance": { "id": 1, "field1": "value1", "field2": "value2" }
}

This is only a basic example. For more details, including how to send arguments and parameters along with subscriptions, read the Tutorial and the Websocket API Reference.

Closing Notes

django-rest-live took initial inspiration from this article by Kit La Touche. Differently from projects like djangochannelsrestframework, django-rest-live does not aim to supplant REST Framework for performing CRUD actions through a REST API. Instead, it is designed to be used in conjunction with HTTP REST endpoints. Clients should still use normal REST framework endpoints generated by ViewSets and other API views to get initial data to populate a page, as well as any write-driven behavior (POST, PATCH, PUT, DELETE). django-rest-live gets rid of the need for periodic polling GET requests to for resource updates after page load.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].