Code Monkey home page Code Monkey logo

django-grip's Introduction

Django GRIP

Author: Justin Karneges [email protected]

This library helps your Django application delegate long-lived HTTP/WebSocket connection management to a GRIP-compatible proxy such as Fanout Cloud or Pushpin.

Setup

First, install this module:

pip install django-grip

In your settings.py, add the GripMiddleware:

MIDDLEWARE = [
    'django_grip.GripMiddleware',
    ...
]

The middleware handles parsing/generating GRIP headers and WebSocket-Over-HTTP events. It should be placed early in the stack.

Additionally, set GRIP_URL with your proxy settings, e.g.:

# fanout cloud
GRIP_URL = 'http://api.fanout.io/realm/your-realm?iss=your-realm&key=base64:your-realm-key'
# pushpin
GRIP_URL = 'http://localhost:5561'

Usage

HTTP streaming

from django.http import HttpResponse, HttpResponseNotAllowed
from gripcontrol import HttpStreamFormat
from django_grip import set_hold_stream, publish

def myendpoint(request):
    if request.method == 'GET':
        # if the request didn't come through a GRIP proxy, throw 501
        if not request.grip_proxied:
            return HttpResponse('Not Implemented\n', status=501)

        # subscribe every incoming request to a channel in stream mode
        resp = HttpResponse('[stream open]\n', content_type='text/plain')
        set_hold_stream(request, 'test')
        return resp
    elif request.method == 'POST':
        # publish data to subscribers
        data = request.POST['data']
        publish('test', HttpStreamFormat(data + '\n'))
        return HttpResponse('Ok\n')
    else:
        return HttpResponseNotAllowed(['GET', 'POST'])

HTTP long-polling

from django.http import HttpResponse, HttpResponseNotModified, HttpResponseNotAllowed
from gripcontrol import HttpResponseFormat
from django_grip import set_hold_response, publish

def myendpoint(request):
    if request.method == 'GET':
        # get object and its etag
        obj = ...
        etag = ...

        # if object is unchanged, long-poll, else return object
        inm = request.META.get('HTTP_IF_NONE_MATCH')
        if inm == etag:
            # subscribe request to channel, return status 304 after timeout
            resp = HttpResponseNotModified()
            set_hold_longpoll(request, 'test')
        else:
            resp = HttpResponse(obj.serialize())

        resp['ETag'] = etag
        return resp
    elif request.method == 'POST':
        data = request.POST['data']

        # update object based on request, and get resulting object and its etag
        obj = ...
        etag = ...

        # publish data to subscribers
        headers = {'ETag': etag}
        publish('test', HttpResponseFormat(obj.serialize(), headers=headers))
        return HttpResponse('Ok\n')
    else:
        return HttpResponseNotAllowed(['GET', 'POST'])

WebSockets

Here's an echo service with a broadcast endpoint:

from django.http import HttpResponse, HttpResponseNotAllowed
from gripcontrol import WebSocketMessageFormat
from django_grip import websocket_only, publish

# decorator means reject non-websocket-related requests. it also means we
#   don't need to return an HttpResponse object. the middleware will take care
#   of that for us.
@websocket_only
def echo(request):
    # since we used the decorator, this will always be a non-None value
    ws = request.wscontext

    # if this is a new connection, accept it and subscribe it to a channel
    if ws.is_opening():
        ws.accept()
        ws.subscribe('test')

    # here we loop over any messages
    while ws.can_recv():
        message = ws.recv()

        # if return value is None, then the connection is closed
        if message is None:
            ws.close()
            break

        # echo the message
        ws.send(message)

def broadcast(request):
    if request.method == 'POST':
        # publish data to all clients that are connected to the echo endpoint
        data = request.POST['data']
        publish('test', WebSocketMessageFormat(data))
        return HttpResponse('Ok\n')
    else:
        return HttpResponseNotAllowed(['POST'])

The while loop is deceptive. It looks like it's looping for the lifetime of the WebSocket connection, but what it's really doing is looping through a batch of WebSocket messages that was just received via HTTP. Often this will be one message, and so the loop performs one iteration and then exits. Similarly, the ws object only exists for the duration of the handler invocation, rather than for the lifetime of the connection as you might expect. It may look like socket code, but it's all an illusion. ๐ŸŽฉ

For details on the underlying protocol conversion, see the WebSocket-Over-HTTP Protocol spec.

Advanced settings

If you need to communicate with more than one GRIP proxy (e.g. multiple Pushpin instances, or Fanout Cloud + Pushpin), you can use GRIP_PROXIES instead of GRIP_URL. For example:

GRIP_PROXIES = [
    # pushpin
    {
        'control_uri': 'http://localhost:5561'
    },
    # fanout cloud
    {
        'key': b64decode('your-realm-key'),
        'control_uri': 'http://api.fanout.io/realm/your-realm',
        'control_iss': 'your-realm'
    }
]

If it's possible for clients to access the Django app directly, without necessarily going through a GRIP proxy, then you may want to avoid sending GRIP instructions to those clients. An easy way to achieve this is with the GRIP_PROXY_REQUIRED setting. If set, then any direct requests that trigger a GRIP instruction response will be given a 501 Not Implemented error instead.

GRIP_PROXY_REQUIRED = True

To prepend a fixed string to all channels used for publishing and subscribing, set GRIP_PREFIX in your configuration:

GRIP_PREFIX = 'myapp-'

django-grip's People

Contributors

jkarneges avatar jzmiller1 avatar maxhowald 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.