Push live updates to client through Django Channels & Websockets

前端 未结 1 657
南笙
南笙 2021-02-06 15:05

I\'m trying to make a page which shows live-updating data to the client. The rest of the site is built with Django, so I\'m trying to use Channels for this.

The data I

1条回答
  •  不思量自难忘°
    2021-02-06 15:35

    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'})
    

    0 讨论(0)
提交回复
热议问题