Giter Site home page Giter Site logo

django-rest-framework-reactive's Introduction

Django REST Framework Reactive

Build Status Documentation Status Version on PyPI Supported Python versions Number of downloads from PyPI Code style: black

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

django-rest-framework-reactive's People

Contributors

hadalin avatar kostko avatar mstajdohar avatar tjanez avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.