question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Send events from management commands

See original GitHub issue

Hey, 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:open
  • Created a year ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
microHoffmancommented, May 18, 2022

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: django/channels#1302 (comment) .

I see, thanks for the pointer! So with that how do you send something from a management command to the consumer, are there any intricacies to it?

You just (im not an expert so hopefully this makes sense:D):

  • define asgi application
  • setup your subscription route (i’ve done it in routing in asgi.py). add a corresponding SseConsumer (the one taken form the django channels GH) to this route
  • in the consumer, i am adding user to the group in handle method - like this await self.channel_layer.group_add(group_name, self.channel_name) - you can access the self.channel_layer in the SseConsumer directly
  • then when i want to send events (e.g. from management command), I am using this group send:
        from channels.layers import get_channel_layer
        self.channel_layer = get_channel_layer()
        async_to_sync(self.channel_layer.group_send)(recipient_address, {"type": type, "data": data})

Hopefully this makes sense:D

1reaction
microHoffmancommented, May 17, 2022

@microHoffman Could you share some more details as how you managed to get the interprocess communication working? Thanks!

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 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send Events Last Updated July 19, 2022 - TechDocs
The sendevent command lets you manually generate events. Use manually generated events to complete certain system maintenance and workload ...
Read more >
Scheduling Custom Django Management Commands
Running scheduled tasks that rely on data or code in your Django application is simple with custom `django-admin` commands and Heroku ...
Read more >
Writing Custom Management Commands in Django - Medium
To write a command we have to define a Command class that inherits from BaseCommand class. To do that let's import BaseCommand and...
Read more >
Event System Management Commands - Cisco
Enables DPE to trigger events, which involves common interface to enable DPE events features and depends on the below event type. It triggers...
Read more >
mposter and msend - BMC Documentation
The mposter and msend commands can both be used to send or modify events, but mposter also can be used to send or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found