niwibe/django-sse — pull request #2 by fcurella (2012-07-12 17:10:50+00:00)
Title: Features/redis

I've added the option to specific a custom response class for the decorator, and added a response class that can subscribe to Redis.

you can specify how to connect to Redis via the ``SSE_REDIS_BROKER`` setting, which defaults to:

SSE_REDIS_BROKER = {
'HOST': 'localhost',
'PORT': 6379,
'DB': 0
}

then, you can implement your view this way:

from django_sse.decorators import is_stream_sse_method
from django_sse.response import RedisPubSubResponse

class SSEView(View):
http_method_names = ['get', ]

@is_stream_sse_method(response_class=RedisPubSubResponse, sleep=1)
def get(self, request, *args, **kwargs):
now_date = unicode(now())
self.sse.event_date(text=now_date)

you can push events to clients from anywhere in your code by using ``redis_add_message``:

from django_sse.utils import redis_add_message

redis_add_message('event_name', 'text')