I'm trying to send notification from backend (Django) to frontend (Vue.js) using django_eventstream
Following the library quide, I create endpoint in asgi.py:
import django_eventstream
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
django_asgi_app = get_asgi_application()
from django.urls import path, re_path
import sv.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = ProtocolTypeRouter({
"http": URLRouter([
path(
'frontend-notifications/',
AuthMiddlewareStack(
URLRouter(django_eventstream.routing.urlpatterns)
),
{
'channels': ['test'],
}),
re_path(r'', django_asgi_app),
]),
"websocket": URLRouter(sv.routing.websocket_urlpatterns),
})
At the frontend:
const es = new ReconnectingEventSource(url+ '/frontend-notifications/');
es.addEventListener('success', function (e) {
const data = JSON.parse(e.data);
console.log('message', data);
}, false );
The event stream channel is established, it can be seen in the browser debug panel.
Then at the backend I send an event to the stream:
from django_eventstream import send_event
send_event(
"test",
"success",
{
"success": True
}
)
But there is no data in the stream.
I've tried to send data to the stream via requests as well:
import requests
session = requests.Session()
r = session.post(url, headers={'Connection': 'keep-alive', 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache'}, data='data: Hello World\n\n', stream=True)
The request returns 200 OK, but again no any data in the stream.
How to send notification from back to front via SSE? Working example would be great.