Skip to main content

Making Django REST Framework reactive

Project description

Build Status

This package enables regular Django REST Framework views to become reactive, that is so that client-side applications may get notified of changes to the underlying data as soon as they happen, without the need to poll the API again. While the initial request is done as a regular HTTP request, all the update notifications come through WebSockets.

Install

Prerequisites

The reactive extensions for Django REST Framework require the use of Django Channels for push notifications via WebSockets.

From PyPI

pip install djangorestframework-reactive

From source

pip install https://github.com/genialis/django-rest-framework-reactive/archive/<git-tree-ish>.tar.gz

where <git-tree-ish> can represent any commit SHA, branch name, tag name, etc. in DRF Reactive’s GitHub repository. For example, to install the latest version from the master branch, use:

pip install https://github.com/genialis/django-rest-framework-reactive/archive/master.tar.gz

Configure

First, add rest_framework_reactive to INSTALLED_APPS.

Configure your Django Channels routing.py to include the required paths:

from django.conf.urls import url

from channels.routing import ChannelNameRouter, ProtocolTypeRouter, URLRouter

from rest_framework_reactive.consumers import ClientConsumer, PollObserversConsumer, WorkerConsumer
from rest_framework_reactive.protocol import CHANNEL_POLL_OBSERVER, CHANNEL_WORKER_NOTIFY

application = ProtocolTypeRouter({
    # Client-facing consumers.
    'websocket': URLRouter([
        # To change the prefix, you can import ClientConsumer in your custom
        # Channels routing definitions instead of using these defaults.
        url(r'^ws/(?P<subscriber_id>.+)$', ClientConsumer),
    ]),

    # Background worker consumers.
    'channel': ChannelNameRouter({
        CHANNEL_POLL_OBSERVER: PollObserversConsumer,
        CHANNEL_WORKER_NOTIFY: WorkerConsumer,
    })
})

Also, urls.py need to be updated to include some additional paths:

urlpatterns = [
  # ...
  url(r'^api/queryobserver/', include('rest_framework_reactive.api_urls')),
  # ...
]

Run

In addition to running a Django application server instance, you need to also run a separate observer worker process (or multiple of them). You may start it by running:

python manage.py runworker rest_framework_reactive.worker rest_framework_reactive.poll_observer

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangorestframework-reactive-3.0.3.tar.gz (18.9 kB view details)

Uploaded Source

File details

Details for the file djangorestframework-reactive-3.0.3.tar.gz.

File metadata

File hashes

Hashes for djangorestframework-reactive-3.0.3.tar.gz
Algorithm Hash digest
SHA256 cf8d9bd8d5c1e78548fb58ec7c3fcd78c796ffc13b6264393da4d001a4a6ca47
MD5 24c1d2bdc727402b3081e816cd93fe35
BLAKE2b-256 35f0907924a684574e36ff05e3ad9306ac5baae3596784ae5c0ca2099a6fced3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page