I am trying to connect browsers with a server that provides Server Sent Events (SSE). This server has a different domain than the original one. For example if you call http://d1.example.com/page
this page will try to connect to an SSE channel on http://d2.example.com/subscribe
. Trying to do that will prompt the following error:
Uncaught Error: SECURITY_ERR: DOM Exception 18
on the line:
var source = new EventSource("http://d2.example.com/subscribe")
How can I fix that?
Update (Solutions that I have tried out):
1- CORS
I tried CORS by adding Access-Control-Allow-Origin:*
to the headers of my web service d2.example.com
. It did not solve EventSource
problem, even though $.get("http://d2.example.com")
calls from d1.example.com
pages are working fine now! I thought SSE works on normal HTTP requests, so why is it not working on Chrome?
2- Redirect
I am using httpd server, so I created a redirect rule in d1.example.com
virtual host that passes SSE requests to d2.example.com
. It worked perfectly with Firefox. Chrome on the other hand did not prompt any error and did not connect to the SSE server either. It looks like it dumped the whole EventSource
command. It looks like this solution will never work, so lets move on...
3- Reverse proxy
Both browsers connected on d1.example.com/subscribe
which is basically connecting to d2.example.com
through reverse proxy. But on_close
event is never caught, even if the browser is closed. Which makes sense since the d2 server is now making the channel with the proxy server. How can I forward the on_close
event from proxy server to d2?
Is there any different ways that could make this work?
The odds are high this is an issue of cross-origin resource sharing, which you must enable on the relevant domains.
http://enable-cors.org/
As the MDN says:
Note: Although it's not yet part of the standard, EventSource supports CORS in Firefox 11 and later. This is expected to become standard soon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With