fanout / django-eventstream

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

get stuck with send_event #92

Open ikrma47 opened 2 years ago

ikrma47 commented 2 years ago

Hello, I've been stuck with send_event function.

my backend is generating image frames after processing a video, I want to send these frames directly to frontend. but I tried using this library.

whenever I send a get request to /events/ it displayed

event: stream-open data

after that when I use send_event function with type that I specified in the urlpatterns, but unfortunately nothing happens

path('events/', include(django_eventstream.urls), {'channels': 'test'}),
def get(self, request):

        send_event('test', 'message', {"test": "asdsa"})
        return HttpResponse()

whenever I hit this view, I received nothing on the frontend. my frontend is react

useEffect(() => {
    const eventSource = new EventSource("http://localhost:8000/events/");
    eventSource.onopen = (e) => console.log("opened");
    eventSource.onmessage = (e) => console.log(e);
  }, []);
jkarneges commented 2 years ago

I notice you're setting a path entry, which is usually part of urls.py and used for WSGI setups. However, Django Channels requires setting up ASGI-based routes. See for example: https://github.com/fanout/django-eventstream/blob/master/examples/chat/server/asgi.py#L30

ikrma47 commented 2 years ago

Got it. Now it's working fine.

Thank you very much. This library makes my FYP easier.

xtlc commented 1 year ago

I think I am running in the same problem. I am a little bit confused, because @jkarneges you write "not to set a path entry in urls.py" but in the linked project there is a path object set in the urls.py:

urlpatterns = [
    path('', views.home),
    path('<room_id>', views.home),
    path('rooms/<room_id>/messages/', views.messages),
    path('rooms/<room_id>/events/', include(django_eventstream.urls), {'format-channels': ['room-{room_id}']}),
    ]
jkarneges commented 1 year ago

@xtlc the project is set up for both ASGI and WSGI, where urls.py is used by the latter.