Open
Description
Hello,
I've been stuck at cors issue with reconnecting event source.
All my others API's are working fine but when I use ReconnectingEventSource I start facing Cors error.
my code at frontend with react
function App() {
useEffect(() => {
let es = new ReconnectingEventSource("http://localhost:8000/events/")
es.onopen = function (){
console.log("connected")
}
},[])
}
backend code
settings.py
CORS_ORIGIN_ALLOW_ALL = True
ALLOWED_HOSTS = ["localhost"]
CORS_ALLOWED_ORIGINS = ["http://localhost"]
CORS_ALLOW_CREDENTIALS = False
INSTALLED_APPS = [
....
'channels',
'django_eventstream',
'corsheaders',
....
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
....
]
asgi.py
import os
from django.core.asgi import get_asgi_application
from django.conf.urls import url
from channels.routing import ProtocolTypeRouter, URLRouter
# from channels.auth import AuthMiddlewareStack
import django_eventstream
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = ProtocolTypeRouter({
'http': URLRouter([
url(r'^events/',
URLRouter(django_eventstream.routing.urlpatterns)
, { 'channels': ['test'] }),
url(r'', get_asgi_application()),
]),
})
if work with templates in django, it works very well without any error. but I want to use react at my frontend side. please let me know how to fix this error. I'm stuck at it very badly.
Metadata
Metadata
Assignees
Labels
No labels