• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 13 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTML5 Server-Sent Events integration for Django

django-sse

Warning: this package is now unmantained because django is not the optimal platform for persistent connections. I strongly recommend use anything like tornado or asyncio with python3 for this purpose.

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

More Repositories

1

phantompy

Phantompy is a headless WebKit engine with powerful pythonic api build on top of Qt5 Webkit
C++
617
star
2

django-jinja

Simple and nonobstructive jinja2 integration with Django.
Python
363
star
3

jnanomsg

Clojure and Java bindings for nanomsg (build on top of JNA).
Java
118
star
4

djorm-pgarray

PostgreSQL array field for Django.
Python
114
star
5

djangorestframework-composed-permissions

A simple way to define complex permissions for django-rest-framework
Python
79
star
6

sse

Server Sent Events protocol implemetation on python2/3
Python
67
star
7

djorm-ext-expressions

Expressions module of django orm extensions package (collection of third party plugins build in one unified package)
Python
51
star
8

djorm-ext-core

Core module of django orm extensions package. (Collection of third party plugins build in one unified package)
Python
27
star
9

django-sites

Alternative implementation of django "sites" framework based on settings instead of models.
Python
24
star
10

django-greenqueue

Asynchronous task task/job queue based on distributed message passing.
Python
23
star
11

tornado-webtools

Microframework for python 3 based on tornado, jinja2 and sqlalchemy.
Python
19
star
12

Green-Mine

Project management web application with scrum in the mind! Build on top of Django
JavaScript
17
star
13

cljs-workshop

Clojure
14
star
14

djorm-ext-pgjson

PostgreSQL native json field support for Django.
Python
14
star
15

djorm-ext-pgbytea

Django extension for postgresql bytea field and other usefull toos for dealing with binary data.
Python
11
star
16

pgbouncer-ng

Simple connection pool for postgresql, written in python.
Python
10
star
17

niwi-web

Source code of www.niwi.be
Python
8
star
18

pyssh-ctypes

Is a python, object oriented wrapper for libssh build with ctypes.
Python
8
star
19

djorm-ext-pguuid

PostgreSQL native uuid field support for Django.
Python
7
star
20

django-ws

Python
6
star
21

django-rawinclude

Small module for django that gives the ease of loading templates in raw. This really is useful for embedding javascript templates with sintaxys similar to django.
Python
6
star
22

py-libssh

Libssh bindings for python2/3 written with boost::python
C++
6
star
23

django-logstream

Multiprocess log collector for python/django-logging
Python
6
star
24

cljs-workshop-doc

Makefile
5
star
25

env-config

Clojure library for access to environment dependent configuration.
Clojure
5
star
26

djorm-ext-pggeom

PostgreSQL native geometric types and fields extension for Django.
Python
5
star
27

cljsoup

A clojure library for parse HTML (clojure idiomatic wrapper of jsoup)
Clojure
5
star
28

moment-tokens

Unix (strftime) and php format translations for momentjs
JavaScript
4
star
29

niwi-slides

My slides.
JavaScript
4
star
30

rum-playground

My experiments with rum and other cool stuff like cats :D
Clojure
4
star
31

niwibe-site

Source code of my site.
Vim Script
4
star
32

needlestack

Experiment for make more low-level and python3 compatible modular search for Django (like haystack but with other philosophy)
Python
4
star
33

dotfiles

My conf backup
Vim Script
3
star
34

django-dbconf

Simple helper for store some configuration to database and efficient access to this settings.
Python
3
star
35

lang-benchmarks

Groovy
3
star
36

djcities

A full database of contry/state/city/timezone for django.
Python
3
star
37

bytehold

Simple backup tool written in python3, which helps not to repeat scripts.
Python
3
star
38

py-s3put

Scp like, amazon s3 backup tool.
Python
2
star
39

clojure.jdbc-hikari

Fast, simple, reliable. HikariCP is a 'zero-overhead' production ready JDBC connection pool implementation for clojure.jdbc
Clojure
2
star
40

django-jsgettext

Improvement for a javascript gettext support in Django
Python
2
star
41

py-geventserver

Script for launch any wsgi or django application over gevent wsgi server.
Python
2
star
42

py-mhash

MHash ctypes bindings for python3 and python2
Python
2
star
43

ring-pulsar-jetty9-adapter

A Ring adapter that uses jetty 9.x webserver.
Clojure
1
star
44

continuo

Clojure
1
star
45

test

1
star
46

pf-stats-api

JavaScript
1
star
47

migrante

Flexible database migration library for Clojure.
Clojure
1
star
48

clojure.jdbc-dbcp

Apache commons DBCP (JDBC) connection pool implementation for clojure.jdbc
Clojure
1
star