GithubHelp home page GithubHelp logo

amitmnnit / django-sse-wrapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gfronza/django-sse-wrapper

0.0 2.0 0.0 336 KB

A simple wrapper of the sse implementation for django

License: BSD 3-Clause "New" or "Revised" License

Python 81.83% JavaScript 11.45% HTML 6.24% Shell 0.48%

django-sse-wrapper's Introduction

Django SSE Wrapper

Django SSE wrapper is an easy way to implement Server-Sent Events in a Django application. You can find details about SSE at http://www.html5rocks.com/en/tutorials/eventsource/basics/

Installation

Installing from PyPI:

$ pip install django-sse-wrapper

Usage

Django SSE Wrapper exposes a view called EventStreamView that implements the SSE logic. In your urls.py you will specify a pattern like:

from django.conf.urls import patterns, url

from sse_wrapper.views import EventStreamView


urlpatterns = patterns(
    '',
    url(r'^an-event-stream/$',
        EventStreamView.as_view(channel='some-channel-name'),
        name='an_event_stream'),
)

You can also specify a channel extension. This may be useful, for example, when you have an event stream called course_state_stream, but that needs to differ from one course to another (by course_id, for example). The url pattern would look like:

from django.conf.urls import patterns, url

from sse_wrapper.views import EventStreamView


urlpatterns = patterns(
    '',
    url(r'^course-state-stream/(?P<channel_extension>[\w]+)/$',
        EventStreamView.as_view(channel='course-state'),
        name='course_state_stream'),
)

Brokers

For now, I only support two backend brokers: Memory (testing purpose only) and Redis. Soon I'll be adding support to RabbitMQ, ZeroMQ, and others.

In your settings.py you must specify the broker and its properties:

SSE_BACKEND_CLASS = 'sse_wrapper.backends.redis_based.RedisBasedBackend'
SSE_BACKEND_CONNECTION_SETTINGS = {
    'host': 'localhost',
    'port': 6379,
    'db': 0,
}

Production Environment Notes

Because Server-Sent Events are streams of data, they require long-lived connections. You’ll want to use a server can handle large numbers of simultaneous connections. I strongly recomend you to use some gevent WSGI server.

Contributing

Install Vagrant in order to run the example app and test the code.

License

BSD License

Note: this project is based on django-sse written by niwibe. The reason I started a new project instead of forking is that I decided to change the whole design of the code. I like the idea of omitting the broker from the app code, this way when you decide to change from one broker to another, you just have to change your settings.py.

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.