github django html5,GitHub - niwinz/django-sse: 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.

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\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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值