Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get stuck with send_event #92

Open
ikrma47 opened this issue Jun 12, 2022 · 4 comments
Open

get stuck with send_event #92

ikrma47 opened this issue Jun 12, 2022 · 4 comments

Comments

@ikrma47
Copy link

ikrma47 commented Jun 12, 2022

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
Copy link
Member

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
Copy link
Author

ikrma47 commented Jun 30, 2022

Got it. Now it's working fine.

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

@xtlc
Copy link

xtlc commented Feb 8, 2023

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants