0

I have managed to get django sending messages to a browser websocket client using channels. But I don't understand it very well.

Messages my Celery background task creates before the websocket handshake from the browser do not get shown.

messages are sent with

async_to_sync(channel_layer.group_send)(str(job.id),..)

Django v 2.1.7.

The channel layer uses redis so group_send is from channels_redis and the first argument is the group id. I would like a client connecting with the job.id to get all messages ever sent to that group.

Does this make sense?

Tim Richardson
  • 6,608
  • 6
  • 44
  • 71

1 Answers1

0

The messages being sent are not waiting for unknown future clients/connections so you would have to implement that mechanism yourself. You should check if the client is connected before sending the message in which case you should store the message in some table and send it to the client later upon connection.

The main issue is how to track the online status of the client. I use an approach similar to the one in the answer to this question but it is not very reliable but isn't bad for a start

Ken4scholars
  • 6,076
  • 2
  • 21
  • 38
  • OK. I do already store the messages in a DB. I had a 'DIY' websocket server before, that sent all existing messages to new clients when they connected. I will try to work out how to replicate that with Channels. – Tim Richardson Mar 25 '19 at 22:05