Django Channels SSE messages are hit or miss unpredictably

148 Views Asked by At

I am building an app with general django views and DRF views for a Flutter app for the same features. I also have django-channels for sending notifications to clients through Server Sent Events. I have inherited from AsyncHttpConsumer for the SSE consumer and locally everything works perfectly fine.

The entire project is a docker compose setup quickstarted with cookiecutter-django, with redis, celery, django, traefik, and some other containers. I use the channels_redis.core.RedisChannelLayer backend as well.

In my django and DRF views, I call a function I created in the User model to send notifications where I send messages to the channel layer through the group name.

Locally all messages are received as it should, but when I perform the same actions in a staging environment, the messages are a hit or miss. Without any apparent pattern, sometimes messages arrive as it should, other times only one out of 4-5 attempts work, and even other times, none do.

I use the JavaScript EventSource to handle incoming messages.

From the django logs, I can see 4 workers and some debug statements show that one (varying each time) worker receives the event, while the rest prints empty content. (The consumer checks if the message is actually for the user in scope.)

I also need the same feature for the Flutter app, so I thought just a persistent request from the app should suffice, but it is even more hit and miss in the app. Am I supposed to consider something for requests outside the web context? I am not a mobile app developer so I can't tell, but generally is it supposed to work the same way without extra considerations?

I feel like the sse connections are subscribed to one worker although I don't really think that is how it works. From what I learned, workers are used to send messages but clients are connected to consumer directly. Is that a correct understanding?

Any pointers would help. Thank you.

0

There are 0 best solutions below