4 ms·
One of my company's APIs uses SSE, and it's been a big support headache for us, because many people are being corporate firewalls that don't do HTTP/2 or HTTP/3
by atombender 2y ago
One of my company's APIs uses SSE, and it's been a big support headache for us, because many people are being corporate firewalls that don't do HTTP/2 or HTTP/3, and people often open many tabs at the same time. It's unfortunately not possible to detect client-side whether the limit has been reached.
Another drawback of SSE is lack of authorization header support. There are a few polyfills (like this one [1]) that simulate SSE over fetch/XHR, but it would be nice to not need to add the bloat.
[1] https://github.com/EventSource/eventsource https://github.com/EventSource/eventsource
- robocat 2y agoPresumably you try SSE, and on failure fallback to something else like WebSockets? Push seems to require supporting multiple communication protocols to avoid failure modes specific to one protocol - and libraries are complex because of that.
- mardifoufs 2y agoBut then why not just use websockets?
- virtue3 2y agoFrom what I understand websockets are great until you have to load balance them. And then you learn why they aren’t so great.
- com2kid 2y agoI've scaled websockets before, it isn't that hard. You need to scale up before your servers become overloaded, and basically new connections go north to the newly brought up server. It is a different mentality than scaling stateless services but it isn't super duper hard.
- hooli_gan 2y agoCan you suggest some resources to learn more about Websocket scaling? Seems like an interesting topic
- com2kid 2y agoHonestly I just flipped the right bits in the aws load balancer (maintain persistent connections, just the first thing you are told to do when googling aws load balancers and web sockets) and setup the instance scaler to trigger based upon "# open connections / num servers > threshold". Ideally it is based on the rate of incoming connections, but so long as you leave enough headroom when doing the stupid simple scaling rule you should be fine. Just ensure new instances don't take too long to start up.
- hamandcheese 2y agoMy understanding is the hard part about scaling WebSockets is that they are stateful and long lived connections. That is also true of SSE. Is there some other aspect of WebSockets that make them harder to scale than WebSockets? I guess with WebSockets, if you choose to send messages from the client to the server, then you have some additional work that you wouldn't have with SSE.
- nchmy 2y agoFYI, the dev of that library created a new, better Event Source client https://github.com/rexxars/eventsource-client https://github.com/rexxars/eventsource-client
- atombender 2y agoYes, I know. We both work at Sanity, actually! The reason I didn't mention it was that the newer library isn't a straight polyfill; it offers a completely different interface with async support and so on.
- fitsumbelay 2y agoI hate to suggest a solution before testing it myself so apologies in advance but I have a hunch that Broadcast Channel API can help you detect browser tab opens on client side. New tabs won't connect to event source and instead listen for localStorage updates that the first loaded tab makes. https://www.google.com/search?q=can+I+use+BroadcastChannel+API+to+detect+how+many+tabs+a+client+has+open%3F&rlz=1C5CHFA_enUS917US918&oq=can+I+use+BroadcastChannel+API+to+detect+how+many+tabs+a+client+has+open%3F&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIGCAEQRRhA0gEJMTc0MTdqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8 https://www.google.com/search?q=can+I+use+BroadcastChannel+A... The problem in this case is how to handle the first tab closing and re-assign which tab then becomes the new "first" tab that connects to the event source but it may be a LOE to solve. Again apologies for suggesting unproven solutions but at the same time I'm interested in feedback it gets to see if its near the right track
- b4ckup 2y agoThis sounds like a good use case for using a service worker. All tabs talk to the service worker and the worker is the single instance that talks to the backend and can use only one connection. Maybe there are some trade offs for using SSE in web workers, I'm not sure.
- Keithamus 2y agoBroadcastChannel is a better solution for a couple of reasons. Service Workers are better at intercepting network requests and returning items from a cache, there’s some amount of additional effort to do work outside of that. The other thing is they’re a little more difficult to set up. A broadcast channel can be handled in a couple lines of code, easily debuggable as they run on the main thread, and they’re more suited to the purpose.
- fitsumbelay 2y agoagreed. Workers was one of my first thoughts but I think BroadcastChannel delivers with much lower LOE
- 2y ago
- leni536 2y agoSupposedly websockets (the protocol) support authorization headers, but often there are no APIs for that in websocket libraries, so people just abuse the subprotocols header in the handshake.