Send events from management commands
See original GitHub issueHey, I’m trying to implement SSE notifications endpoint . I have the routing setup as follows:
# asgi.py
application = ProtocolTypeRouter({
"http": URLRouter(
[
re_path(r"", get_asgi_application()),
re_path(
r"^notifications/(?P<user_address>0x[0-9A-Fa-f]+)/$",
URLRouter(django_eventstream.routing.urlpatterns),
{"format-channels": ["notifications-{user_address}"]},
),
]
),
})
# urls.py
urlpatterns = [
re_path(r"^notifications/(?P<user_address>0x[0-9A-Fa-f]+)/", include(django_eventstream.urls), {"format-channels": ["notifications-{user_address}"]})
]
The problem here is that I’d like to send the event (via send_event
function) from separate management command, that handles the events and sending the notification and this management command runs on different process. I tried sending it directly from the management command, but unfortunately the event was not sent back to the client. If I understand this correctly, it’s due that runserver
and event_listener
management command run on different processes.
So if I understand properly, I need to somehow pass to the EventsConsumer
class the info that this event happened using django channels and then send the event from this consumer? I was also playing around with extending the EventsConsumer
, but unfortunately I was not able to implement this. If you have a spare minute and you have any idea how I could achieve this (sending events from managment commands), I’d greatly appreciate any help. Thanks!
P.S: I’ve tried Pushpin, seems to be working alright, but ideally we would stick to the channels for more “standard” deployment.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
You just (im not an expert so hopefully this makes sense:D):
asgi.py
). add a correspondingSseConsumer
(the one taken form the django channels GH) to this routehandle
method - like thisawait self.channel_layer.group_add(group_name, self.channel_name)
- you can access theself.channel_layer
in theSseConsumer
directlyHopefully this makes sense:D
Hey @tommyxd , I’ve ended up not using django-eventstream, but rather just django-channels. I’ve utilized the channel layers for the interprocess communication. For the sse consumer itself, I got inspired here: https://github.com/django/channels/issues/1302#issuecomment-508896846 .