Hi - I'm working with Django Channels (1.1.3) on a Django project and I have some questions about how to send multiple messages from the same consumer.
The idea is the consumer will receive a request to do some work, and will send incremental results back to user. I've attached a simplified example below with some questions. I'm hoping someone can steer me in the right direction.
Django Channels:
def do_some_work(message):
for i in range(3):
time.sleep(2)
message.reply_channel.send({"text": 'Finished work ' + str(i)})
def ws_connect(message):
message.reply_channel.send({"accept": True})
channel_routing = [
route('websocket.connect', ws_connect),
route('websocket.receive', do_some_work, path=r'/todo')
]
Javascript (borrowed mostly from the docs):
socket = new WebSocket("ws://" + window.location.host + "/todo/");
socket.onmessage = function(e) {
console.log(e.data);
}
socket.onopen = function() {
socket.send("hello world");
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
Expected results: Every two seconds the browser would receive a reply from the consumer saying 'Finished work x'.
What I observe is a delay for ~6 seconds and then all results returned at the same time. Are the response on the reply channel blocked somehow? Is there I way I can yield, or release the message before doing the next increment of work?
I've also tried setting up a new consumer that only replies via a Group. So instead of message.reply_channel.send, I would Channel('reply-channel').send({results: ...}) and have a new consumer defined on channel_routing for the 'reply-channel'. But the results are the same - all the 'work' is completed before the browser receives a response.
Any hints on how to send multiple message in the same consumer?
Thanks in advance.