4 ms·
> The natural way to use SSE is to create an EventSource on each page. If you do this, and your user opens six tabs, they can now no longer make HTTP requests t
by DaiPlusPlus 3y ago
> The natural way to use SSE is to create an EventSource on each page. If you do this, and your user opens six tabs, they can now no longer make HTTP requests to your site. Not just SSE requests, any HTTP requests at all!
You can use EventSource in a singleton worker and use postMessage to forward messages to all open browser tabs for the same origin.
- ranguna 3y agoThe original comment stands: it's harder.
- tehbeard 3y agoSharedWorker (the thing needed for a "singleton worker") only just made it into Safari the end of last year... plus you now have to handle multiplexing and demultiplexing for different tabs on both server and client. By the time that arrived and HTTP/2 had wide enough adoption, everyone's already used to building atop Websocket... Also the EventSource in the browser is heavily gimped. GET only, No custom headers, and last I checked you can forget about configuring any CORS stuff for credential passing.
- cassepipe 3y agoNot sure if that's what you are asking but there's a `withCredentials` option (which IIRC is the only possible option) I was looking at : https://stackoverflow.com/questions/61041389/managing-server-side-events-with-a-service-worker https://stackoverflow.com/questions/61041389/managing-server... Shared worker would actually allow for SSE in a service worker ! That's nice.
- anshumankmr 3y agoEventSource is bad. We were using it to connect to GPT 4 via our server acting as a middleware (we hosted the code for returning the LLM response on our server since our company's OpenAI API key is supposed to be private). However, previous developers had written a basic middleware to check if a req.body exists and if it contains a userId (since our server was handling multipler other things apart from this one component of the business logic). However, any such manipulation of the request body isn't possible with EventSource (and I felt that using a workaround such as passing the userId in the querystring could cause some security issue), so I ended up using the fetch() method to connect to our server for interacting with GPT 4.
- cassepipe 3y agoThis SO answer seems to say that it wouldn't work : https://stackoverflow.com/questions/61041389/managing-server-side-events-with-a-service-worker https://stackoverflow.com/questions/61041389/managing-server... If you have an answer to that answer I'd be very much interested
- jraph 3y agoYou can use BroadcastChannel [1] to communicate between tabs. It works well and does not require service workers. It's used in Tracim [2], for some reason we could not move to HTTP/2 at the time I was working on this project so we indeed learned about this connection limit soon enough… Even on HTTP/2, I believe it's best avoid sending 6 times the same message to the user just because they opened 6 tabs. It's less wasteful both for the server and the client. [1] https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel https://developer.mozilla.org/en-US/docs/Web/API/BroadcastCh... [2] https://github.com/tracim/tracim/blob/develop/frontend_lib/src/LiveMessageManager.js https://github.com/tracim/tracim/blob/develop/frontend_lib/s...
- cassepipe 3y agoThanks very much for the examples ! I get your point but it does seem quite more involved to do all that management and broadcasting by hand rather than having a shared worker with a EventSource singleton for all, don't you think ? : https://stackoverflow.com/a/61636606/10469162 https://stackoverflow.com/a/61636606/10469162 EDIT: Someone posted this very relevant and interesting post : https://greenvitriol.com/posts/browser-leader https://greenvitriol.com/posts/browser-leader