Coder Social home page Coder Social logo

django-sse's Introduction

django-sse

Django integration with Server-Sent Events. (http://www.html5rocks.com/en/tutorials/eventsource/basics/) (https://developer.mozilla.org/es/docs/Server-sent_events/utilizando_server_sent_events_sse)

This django application uses the module sse, simple python implementation of sse protocol.

Introduction

Is very similar to Django's generic views.

django-sse exposes a generic view for implement the custom logic of the data stream. Additionally, it exposes some helper views for simple enqueuing messages for a client, using redis or rabbitmq(not implemented).

NOTE: it strongly recomended expose this views with gevent pywsgi server, because every connection is permanent blocking stream.

Implementing own view with sample stream

The idea is to create a stream of data to send the current timestamp every 1 second to the client:

from django_sse.views import BaseSseView
import time

class MySseStreamView(BaseSseView):
    def iterator(self):
        while True:
            self.sse.add_message("time", str(time.time()))
            yield
            time.sleep(1)

The iterator() method must be a generator of data stream. The view has sse object, for more information, see sse module documentation.

The acomulated data on sse is flushed to the client every iteration (yield statement). You can flush the buffer, sometimes as you need.

Using a redis as message queue for push messages to client

django-sse currently implements a redis helper for simple enqueuing messages for push to a clients. For use it, the first step is a url declaration:

from django.conf.urls import patterns, include, url
from django_sse.redisqueue import RedisQueueView

urlpatterns = patterns('',
    url(r'^stream1/$', RedisQueueView.as_view(redis_channel="foo"), name="stream1"),
)

This, on new connection is created, opens connection to redis and subscribe to a channel. On new messages received from redis, it flushes theese to client.

And the second step, your can push messages to the queue in any other normal django views with a simple api:

from django.http import HttpResponse
from django_sse.redisqueue import send_event

def someview(request):
    send_event("myevent", "mydata", channel="foo")
    return HttpResponse("dummy response")

RedisQueueView precises of redis, put your connection params on your settings.py:

REDIS_SSEQUEUE_CONNECTION_SETTINGS = {
    'location': 'localhost:6379',
    'db': 0,
}

Can subscribe to a channel dynamically with some parameter on url?

Yes, you need create a subclass of RedisQueueView and overwrite the method get_redis_channel. Example:

# urls.py
urlpatterns = patterns('',
    url(r'^sse/(?P<channel>\w+)/$', MyRedisQueueView.as_view(redis_channel="foo"), name="stream1"),
)

class MyRedisQueueView(RedisQueueView):
    def get_redis_channel(self):
        return self.kwargs['channel'] or self.redis_channel

Contributors:

License

BSD License

django-sse's People

Contributors

aztrock avatar dragozov avatar fcurella avatar lukaszb avatar streeter avatar sv1jsb 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.