Monday, January 2, 2017

How to send user specific data on Group().send() on Channels?

In my application, I need run a piece of code (let's call it prepare_and_send_data(user)) for every online user, every N minutes. That piece of code will do a calculation using the data that belongs to the user, and send the results over a WebSocket connection.

Initially, I thought that Group('online-users').send({'data': 'data'}) sends the data to the websocket.send channel. Hence, I thought that if I write a consumer for the websocket.send channel, I can do my user specific calculations in the consumer for the websocket.send channel. Hence, calling Group('online-users').send({'data': 'data'}) every N minutes would have been enough to get the application going. Explaining with code:

@channel_session_user_from_http
def websocket_connect(message):
    Group('online-users').add(message.reply_channel)

@channel_session_user
def websocket_disconnect(message):
    Group('online-users').discard(message.reply_channel)

@channel_session_user
def websocket_send(message):
    # Do calculations here, using message.user and send it.

# Call Group('online-users').send({'data': 'data'}) every N minutes, which (I thought) will send the data to websocket.send # channel, hence the consumer for websocket.send channel will handle preparing the user specific data accordingly.


However, Group('online-users').send({'data': 'data'}) does not send the data to websocket.send channel. It sends the data directly over the WebSocket.

How should I handle this? It is possible to use a handler for Group('online-users').send({'data': 'data'})? AFAICT, using a Group to hold the online users is the best option, per the documentation. For this reason, I did not consider alternatives such as holding the online users in a database table. Should I use something other than a Group?

Also, if websocket.send channel is not used on Group('online-users').send({'data': 'data'}), then when is this channel used?

Thanks in advance

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7636117a-e2e8-4946-9f56-5128caa75709%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment