fanout / django-eventstream

Server-Sent Events for Django
MIT License
638 stars 84 forks source link

Every other event pattern listening to the stream #55

Open MartaLopesGomes opened 4 years ago

MartaLopesGomes commented 4 years ago

Hi,

I followed your instructions on the setup using Django channels 2. Everything seems to be working great and I have my /events/ stream working. I started sending events to the stream and I noticed something. I’m sending JSON objects like these (all with same event type), separately:

{ “message”: “1”} { “message”: “2”} {“message”: “3”} { “message”: “4”} {“message”: “5”}

However, the events I listen from the stream are sent in an every other event pattern, like:

{ “message”: “1”} { “message”: “3”} {“message”: “5”}

I couldn’t find any similar issue, and I can’t understand why I’m getting this pattern every time. Could you help me understand what is the origin of the problem? Could you also suggest a way/tool to debug whats happening in the stream, because I can’t have any information about what is happening after I call the send_event function, and this is making the debug almost impossible...

I'm using Django 3. Could that be a problem? I didn't see any specification of the required Django version.

jkarneges commented 4 years ago

Hi,

Are you seeing this behavior when running in development mode with runserver? Or is this happening with a daphne deployment, and possibly you're running multiple instances?

MartaLopesGomes commented 4 years ago

I'm running in development mode with runserver.

jkarneges commented 4 years ago

I tried running the time example (in this repo) using the latest versions of all dependencies, including Django 3, and it seemed to work fine. Does it work for you too?

MartaLopesGomes commented 4 years ago

I tried running the chat example, with Django 3 and it worked fine.

jkarneges commented 4 years ago

Debugging further is difficult. The library could probably use some debug logging.

All I can suggest is to use curl to connect to the events endpoint in order to examine the raw stream. Also try to think about what is different with your app vs the chat example (use of threads, etc).

MartaLopesGomes commented 4 years ago

I got the same result as before using curl.

I'm using a separate thread to call _sendevent(...). Do you think that can be the reason to observe this pattern? If yes, can you explain or do you know why that can be a problem?

jkarneges commented 4 years ago

send_event is intended to be thread safe, and the time example calls it from a separate thread too, so it should be okay. But perhaps there is some extra complexity in your app that could be interfering. Are you sure send_event is actually getting called the number of times you expect? Are you using some kind of parallelization library that converts threads into processes?

jkarneges commented 4 years ago

Can you trim your code down to a reproducible example and share it?

MartaLopesGomes commented 4 years ago

I was sure that I was calling the send_event the number of times I was expecting.

I think I have the problem solved. But I'm not sure that I know exactly what was causing this issue.

The code that was calling the send_event was being initiated in the AppConfig.ready(). I don't know if this function was being initiated more than one time. Once I started calling python manage.py runserver with the option --noreload, the stream started receiving all the events as it should.

I think it is a solution for now, but I would like to understand better what was happening. I would prefer to be able to use the runserver without the option --noreload.

Anyway, Thanks for your help!

jkarneges commented 4 years ago

Glad to hear you've found a solution. I tried moving the time example's thread init to AppConfig.ready() and it still worked okay, even after modifying a file and letting it reload. An asgiref error was logged though.

Django's runserver reloading is tricky business and it wouldn't surprise me if there is some bug with it or in combination with Channels. Maybe upgrade your Daphne: https://github.com/django/channels/issues/1374