Push live updates to client through Django Channels & Websockets

╄→гoц情女王★ 提交于 2019-12-03 20:12:16

Add user to django channels Group on ws_connect

from channels.auth import channel_session_user_from_http
from channels import Group

@channel_session_user_from_http
def ws_connect(message, **kwargs):
    http_user = message.user
    if not http_user.is_anonymous:
        message.reply_channel.send({'accept': True})
        Group('user-'+str(http_user.id)).add(message.reply_channel)`

send any new updates data to the user by

Group('user-1').send({'text': 'hello user 1'})
Group('user-2').send({'text': 'hello user 2'})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!