TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Server-Sent Events: an alternative to WebSockets

540 pointsby tyrionover 3 years ago

49 comments

bullenover 3 years ago
I made the backend for this MMO on SSE over HTTP&#x2F;1.1:<p><a href="https:&#x2F;&#x2F;store.steampowered.com&#x2F;app&#x2F;486310&#x2F;Meadow&#x2F;" rel="nofollow">https:&#x2F;&#x2F;store.steampowered.com&#x2F;app&#x2F;486310&#x2F;Meadow&#x2F;</a><p>We have had a total of 350.000 players over 6 years and the backend out-scales all other multiplayer servers that exist and it&#x27;s open source:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;tinspin&#x2F;fuse" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tinspin&#x2F;fuse</a><p>You don&#x27;t need HTTP&#x2F;2 to make SSE work well. Actually the HTTP&#x2F;2 TCP head-of-line issue and all the workarounds for that probably make it harder to scale without technical debt.
评论 #30315631 未加载
评论 #30314182 未加载
评论 #30313750 未加载
评论 #30316614 未加载
评论 #30318721 未加载
评论 #30317155 未加载
评论 #30316160 未加载
评论 #30314139 未加载
评论 #30313882 未加载
mmcclimonover 3 years ago
SSEs are one of the standard push mechanisms in JMAP [1], and they&#x27;re part of what make the Fastmail UI so fast. They&#x27;re straightforward to implement, for both server and client, and the only thing I don&#x27;t like about them is that Firefox dev tools make them totally impossible to debug.<p>1. <a href="https:&#x2F;&#x2F;jmap.io&#x2F;spec-core.html#event-source" rel="nofollow">https:&#x2F;&#x2F;jmap.io&#x2F;spec-core.html#event-source</a>
评论 #30321561 未加载
评论 #30315314 未加载
评论 #30318174 未加载
评论 #30316947 未加载
mythzover 3 years ago
We use SSE for our APIs Server Events feature <a href="https:&#x2F;&#x2F;docs.servicestack.net&#x2F;server-events" rel="nofollow">https:&#x2F;&#x2F;docs.servicestack.net&#x2F;server-events</a> with C#, JS&#x2F;TypeScript and Java high-level clients.<p>It&#x27;s a beautifully simple &amp; elegant lightweight push events option that works over standard HTTP, the main gotcha for maintaining long-lived connections is that server&#x2F;clients should implement their own heartbeat to be able to detect &amp; auto reconnect failed connections which was the only reliable way we&#x27;ve found to detect &amp; resolve broken connections.
评论 #30316383 未加载
评论 #30318886 未加载
szastamastaover 3 years ago
My experience with sse is pretty bad. They are unreliable, don’t support headers and require keep-alive hackery. In my experience WebSockets are so much better.<p>Also ease of use doesn’t really convince me. It’s like 5 lines of code with socket.io to have working websockets, without all the downsides of sse.
评论 #30314736 未加载
评论 #30314703 未加载
评论 #30313827 未加载
评论 #30370180 未加载
评论 #30313960 未加载
评论 #30313945 未加载
评论 #30323236 未加载
leeoniyaover 3 years ago
the biggest drawback with SSE, even when unidirectional comm is sufficient is<p>&gt; SSE is subject to limitation with regards to the maximum number of open connections. This can be especially painful when opening various tabs as the limit is per browser and set to a very low number (6).<p><a href="https:&#x2F;&#x2F;ably.com&#x2F;blog&#x2F;websockets-vs-sse" rel="nofollow">https:&#x2F;&#x2F;ably.com&#x2F;blog&#x2F;websockets-vs-sse</a><p>SharedWorker could be one way to solve this, but lack of Safari support is a blocker, as usual. <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;SharedWorker" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;SharedWorke...</a><p>also, for websockets, there are various libs that handle auto-reconnnects<p><a href="https:&#x2F;&#x2F;github.com&#x2F;github&#x2F;stable-socket" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;github&#x2F;stable-socket</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;joewalnes&#x2F;reconnecting-websocket" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;joewalnes&#x2F;reconnecting-websocket</a><p><a href="https:&#x2F;&#x2F;dev.to&#x2F;jeroendk&#x2F;how-to-implement-a-random-exponential-backoff-algorithm-in-javascript-18n6" rel="nofollow">https:&#x2F;&#x2F;dev.to&#x2F;jeroendk&#x2F;how-to-implement-a-random-exponentia...</a>
评论 #30314816 未加载
评论 #30313944 未加载
评论 #30318962 未加载
hishampover 3 years ago
We moved away from WebSockets to SSE, realised it wasn&#x27;t makings thing any better. In fact, it made things worse, so we switched back to WebSockets again and worked on scaling WebSockets. SSE will work much better for other cases, just didn&#x27;t work out for our case.<p>First reason was that it was an array of connections you loop through to broadcast some data. We had around 2000 active connections and needed a less than 1000ms latency, with WebSocket, even though we faced connections drops, client received data on time. But in SSE, it took many seconds to reach some clients, since the data was time critical, WebSocket seemed much easier to scale for our purposes. Another issue was that SSE is like an idea you get done with HTTP APIs, so it doesn&#x27;t have much support around it like WS. Things like rooms, clientIds etc needed to be managed manually, which was also a quite big task by itself. And a few other minor reasons too combined made us switch back to WS.<p>I think SSE will suit much better for connections where bulk broadcast is less, like in shared docs editing, showing stuff like &quot;1234 users is watching this product&quot; etc. And keep in mind that all this is coming from a mediocre full stack developer with 3 YOE only, so take it with a grain of salt.
评论 #30317085 未加载
评论 #30317293 未加载
评论 #30317358 未加载
评论 #30317098 未加载
rawoke083600over 3 years ago
I like them, they surprisingly easy to use..<p>One example where i found it to be not the <i>perfect</i> solution was with a web turn-based game.<p>The SSE was perfect to update gamestate to all clients, but to have great latency from the players point of view whenever the player had to do something, it was via a normal ajax-http call.<p>Eventually I had to switch to <i>uglier</i> websockets and keep connection open.<p>Http-keep-alive was that reliable.
评论 #30314267 未加载
评论 #30313415 未加载
评论 #30317239 未加载
kreetxover 3 years ago
SSEs had a severe connection limit, something like 4 connections per domain per browser (IIRC), so if you had four tabs open then opening new ones would fail.
评论 #30314120 未加载
评论 #30314071 未加载
评论 #30318169 未加载
mmzeemanover 3 years ago
Did research on SSE a short while ago. Found out that the mimetype &quot;text&#x2F;event-stream&quot; was blocked by a couple of anti-virus products. So that was a no-go for us.
评论 #30314799 未加载
评论 #30313846 未加载
评论 #30313978 未加载
评论 #30313891 未加载
评论 #30313753 未加载
foxbarringtonover 3 years ago
I’m a huge fan of SSE. In the first chapter of my book Fullstack Node.js I use it for the real-time chat example because it requires almost zero setup. I’ve also been using SSE on <a href="https:&#x2F;&#x2F;rambly.app" rel="nofollow">https:&#x2F;&#x2F;rambly.app</a> to handle all the WebRTC signaling so that clients can find new peers. Works great.
评论 #30314013 未加载
julianlamover 3 years ago
This is really interesting! I wonder why it never really took off, whereas websockets via Socket.IO&#x2F;Engine.io did.<p>At NodeBB, we ended up relying on websockets for almost everything, which was a mistake. We were using it for simple call-and-response actions, where a proper RESTful API would&#x27;ve been a better (more scalable, better supported, etc.) solution.<p>In the end, we migrated a large part of our existing socket.io implementation to use plain REST. SSE sounds like the second part of that solution, so we can ditch socket.io completely if we really wanted to.<p>Very cool!
评论 #30315890 未加载
samwillisover 3 years ago
I have used SSEs extensively, I think they are brilliant and massively underused.<p>The one thing I wish they supported was a binary event data type (mixed in with text events), effectively being able to send in my case image data as an event. The only way to do it currently is as a Base64 string.
评论 #30314469 未加载
评论 #30314111 未加载
dpwebover 3 years ago
Very easy to implement - still using code I wrote 8 years ago, which is like 20 lines client and server, choosing it at the time over ws.<p>Essentially just new EventSource(), text&#x2F;event-stream header, and keep conn open. Zero dependencies in browser and nodejs. Needs no separate auth.
oneweekwonderover 3 years ago
Personally i use mqtt over websockets, paho[0] is a good js library. It support last will for dc&#x27;s and the message queue design makes it easy to think of and debug. There also a lot of mq brokers that will scale well.<p>[0]: <a href="https:&#x2F;&#x2F;www.eclipse.org&#x2F;paho&#x2F;index.php?page=clients&#x2F;js&#x2F;index.php" rel="nofollow">https:&#x2F;&#x2F;www.eclipse.org&#x2F;paho&#x2F;index.php?page=clients&#x2F;js&#x2F;index...</a>
alin23over 3 years ago
ESPHome (an easy to use firmware for ESP32 chips) uses SSE to send sensor data to subscribers.<p>I made use of that in Lunar (<a href="https:&#x2F;&#x2F;lunar.fyi&#x2F;#sensor" rel="nofollow">https:&#x2F;&#x2F;lunar.fyi&#x2F;#sensor</a>) to be able to adjust monitor brightness based on ambient light readings from an external wireless sensor.<p>At first it felt weird that I have to wait for responses instead of polling with requests myself, but the ESP is not a very powerful chip and making one HTTP request every second would have been too much.<p>SSE also allows the sensor to compare previous readings and only send data when something changed, which removes some of the complexity with debouncing in the app code.
rough-seaover 3 years ago
A complete SSE example in 25 lines on Deno Deploy: <a href="https:&#x2F;&#x2F;dash.deno.com&#x2F;playground&#x2F;server-sent-events" rel="nofollow">https:&#x2F;&#x2F;dash.deno.com&#x2F;playground&#x2F;server-sent-events</a>
waylandsmithersover 3 years ago
I had the pleasure of being forced to use in SSE due to working with a proxy that didn&#x27;t support websockets.<p>Personally I think it&#x27;s a great solution for longer running tasks like &quot;Export your data to CSV&quot; when the client just needs to get an update that it&#x27;s done and here&#x27;s the url to download it.
sb8244over 3 years ago
I can’t find any downsides of SSE presented. My experience is that they’re nice in theory but the devils in the details. The biggest issue being that you basically need http&#x2F;2 to make them practical.
评论 #30313370 未加载
评论 #30315313 未加载
U1F984over 3 years ago
The extra setup step for websocket should not be required: <a href="https:&#x2F;&#x2F;caddyserver.com&#x2F;docs&#x2F;v2-upgrade#proxy" rel="nofollow">https:&#x2F;&#x2F;caddyserver.com&#x2F;docs&#x2F;v2-upgrade#proxy</a><p>I also had no problems with HAProxy, it worked with websockets without any issues or extra handling.
评论 #30318101 未加载
nickjjover 3 years ago
This is why I really really like Hotwire Turbo[0] which is a back-end agnostic way to do fast and partial HTML based page updates over HTTP and it optionally supports broadcasting events with WebSockets (or SSE[1]) only when it makes sense.<p>So many alternatives to Hotwire want to use WebSockets for everything, even for serving HTML from a page transition that&#x27;s not broadcast to anyone. I share the same sentiment as the author in that WebSockets have real pitfalls and I&#x27;d go even further and say unless used tastefully and sparingly they break the whole ethos of the web.<p>HTTP is a rock solid protocol and super optimized &#x2F; well known and easy to scale since it&#x27;s stateless. I hate the idea of going to a site where after it loads, every little component of the page is updated live under my feet. The web is about giving users control. I think the idea of push based updates like showing notifications and other minor updates are great when used in moderation but SSE can do this. I don&#x27;t like the direction of some frameworks around wanting to broadcast everything and use WebSockets to serve HTML to 1 client.<p>I hope in the future Hotwire Turbo alternatives seriously consider using HTTP and SSE as an official transport layer.<p>[0]: <a href="https:&#x2F;&#x2F;hotwired.dev&#x2F;" rel="nofollow">https:&#x2F;&#x2F;hotwired.dev&#x2F;</a><p>[1]: <a href="https:&#x2F;&#x2F;twitter.com&#x2F;dhh&#x2F;status&#x2F;1346095619597889536?lang=en" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;dhh&#x2F;status&#x2F;1346095619597889536?lang=en</a>
ponytechover 3 years ago
One problem I had with WebSockets is you can not set custom HTTP headers when opening the connection. I wanted to implement a JWT based authentication in my backend and had to pass the token either as a query parameter or in a cookie.<p>Anyone knows the rationale behind this limitation?
评论 #30317158 未加载
goodpointover 3 years ago
--- WebSockets cannot benefit from any HTTP feature. That is:<p><pre><code> No support for compression No support for HTTP&#x2F;2 multiplexing Potential issues with proxies No protection from Cross-Site Hijacking</code></pre> ---<p>Is that true? The web never cease to amaze.
评论 #30313364 未加载
评论 #30313403 未加载
quickthrower2over 3 years ago
Is it worth upgrading a long polling solution to SSE? Would I see much benefit?<p>What I mean by that is client sends request, server responds in up to 2 minutes with result or a try again flag. Either way client resends request and then uses response data if provided.
评论 #30313971 未加载
havkomover 3 years ago
The most compatible technique is long polling (with a re-established connection after X seconds if no event). Works suprisingly well in many cases and is not blocket by any proxies.
评论 #30314122 未加载
laerusover 3 years ago
With WebTransport around the corner I don&#x27;t think is worth the time investing in learning a, what seems to me, obsolete technology. I can understand it for already big projects working with SSE that don&#x27;t want to pay the cost of upgrading&#x2F;changing but for anything new I cannot be bothered since Websockets work good enough for my use cases.<p>What worries me though is the trend of dismissal of newer technologies as being useless or bad and the resistance to change.
评论 #30315629 未加载
评论 #30315885 未加载
评论 #30315769 未加载
limaover 3 years ago
One issue with SSE is that dumb enterprise middleboxes and Windows antivirus software break them :(<p>They&#x27;ll try to read the entire stream to completion and will hang forever.
评论 #30313501 未加载
wedn3sdayover 3 years ago
This seems fairly cool, and I appreciate the write up, but god I hate it so much when people write code samples that try and be fancy and use non-code-characters in their code samples. Clarity is much more important then aesthetics when it comes to code examples, if Im trying to understand something I&#x27;ve never seen before, having a bunch of extra non-existant symbols does not help.
评论 #30315126 未加载
评论 #30315183 未加载
评论 #30315119 未加载
评论 #30315112 未加载
Tooover 3 years ago
Can someone give a brief summary of how this differs from long polling. It looks very similar except it has a small layer of formalized event&#x2F;data&#x2F;id structure on top? Are there any differences in the lower connection layers, or any added support by browsers and proxies given some new headers?<p>What are the benefits of SSE vs long polling?
评论 #30314668 未加载
评论 #30315300 未加载
TimWollaover 3 years ago
&gt; RFC 8441, released on September 2018, tries to fix this limitation by adding support for “Bootstrapping WebSockets with HTTP&#x2F;2”. It has been implemented in Firefox and Chrome. However, as far as I know, no major reverse-proxy implements it.<p>HAProxy supports RFC 8441 automatically. It&#x27;s possible to disable it, because support in clients tends to be buggy-ish: <a href="https:&#x2F;&#x2F;cbonte.github.io&#x2F;haproxy-dconv&#x2F;2.4&#x2F;configuration.html#3.1-h2-workaround-bogus-websocket-clients" rel="nofollow">https:&#x2F;&#x2F;cbonte.github.io&#x2F;haproxy-dconv&#x2F;2.4&#x2F;configuration.htm...</a><p>Generally I can second recommendation of using SSE &#x2F; long running response streams over WebSockets for the same reasons as the article.
rcarmoover 3 years ago
I have always preferred SSE to WebSockets. You can do a _lot_ with a minuscule amount of code, and it is great for updating charts and status UIs on the fly without hacking extra ports, server daemons and whatnot.
KaoruAoiShihoover 3 years ago
I have investigated SSE for <a href="https:&#x2F;&#x2F;fiction.live" rel="nofollow">https:&#x2F;&#x2F;fiction.live</a> a few years back but stayed with websockets. Maybe it&#x27;s time for another look. I pay around $300 a month for the websocket server, it&#x27;s probably not worth it yet to try to optimize that but if we keep growing at this rate it may soon be.
tgvover 3 years ago
But SSE is a oneway street, isn’t it? The client gets one chance to send days, and that’s it? Or is there some way around it?
评论 #30316522 未加载
llacb47over 3 years ago
Google uses SSE for hangouts&#x2F;gchat.
ravenstineover 3 years ago
I usually use SSEs for personal projects because they are way more simple than WebSockets (not that those aren&#x27;t also simple) and most of the time my web apps just need to listen for something coming from the server and not bidirectional communication.
andrew_over 3 years ago
EventSource has been around for eons, and is what the precursor to webpack-dev-server used for HMR events. It had the advantage of supporting ancient browsers since the spec has been around a long time and even supported by oldIE.
sysidabout 3 years ago
For starlette&#x2F;fastapi there is a battle tested lib: <a href="https:&#x2F;&#x2F;github.com&#x2F;sysid&#x2F;sse-starlette" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sysid&#x2F;sse-starlette</a>
mterronover 3 years ago
I&#x27;ve found hasses (<a href="https:&#x2F;&#x2F;github.com&#x2F;hyper-prog&#x2F;hasses" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hyper-prog&#x2F;hasses</a>) a really nice SSE server.<p>Good performance, easy to use, easy to integrate.
captn3m0over 3 years ago
I think SSE might make a lot of sense for Serverless workloads? You don&#x27;t have to worry about running a websocket server, any serverless host with HTTP support will do. Long-polling might be costlier though?
jFriedensreichover 3 years ago
this is what i have been telling people for years, but its hard to get the word out there. usually every dev just reflexes without thinking to websockets when anything realtime or push related comes up.
gibsonf1over 3 years ago
Solid has a great solution for this: <a href="https:&#x2F;&#x2F;solid.github.io&#x2F;notifications&#x2F;protocol" rel="nofollow">https:&#x2F;&#x2F;solid.github.io&#x2F;notifications&#x2F;protocol</a>
pbowyerover 3 years ago
There&#x27;s also the Mercure protocol, built on top of Server-Sent Events: <a href="https:&#x2F;&#x2F;mercure.rocks&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mercure.rocks&#x2F;</a>
评论 #30317585 未加载
beebeepkaover 3 years ago
So, what are the downsides to using websockets? They are my go-to solution when I am doing a game, chat, or something else that needs interactivity.
评论 #30313448 未加载
jshenover 3 years ago
Question for those of you who build features on web using things like SSE or web sockets, how do you build those features in native mobile apps?
评论 #30317226 未加载
njxover 3 years ago
My theory why SSE did not take off is because WordPress does not support it.
picturover 3 years ago
Does SSE offer support for capturing connect&#x2F;disconnect situations?
评论 #30313437 未加载
anderspitmanover 3 years ago
My personal browser streaming TL;DR goes something like this:<p>* Start with SSE<p>* If you need to send binary data, use long polling or WebSockets<p>* If you need fast bidi streaming, use WebSockets<p>* If you need backpressure and multiplexing for WebSockets, use RSocket or omnistreams[1] (one of my projects).<p>* Make sure you account for SSE browser connection limits, preferably by minimizing the number of streams needed, or by using HTTP&#x2F;2 (mind head-of-line blocking) or splitting your HTTP&#x2F;1.1 backend across multiple domains and doing round-robin on the frontend.<p>[0]: <a href="https:&#x2F;&#x2F;rsocket.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rsocket.io&#x2F;</a><p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;omnistreams&#x2F;omnistreams-spec" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;omnistreams&#x2F;omnistreams-spec</a>
whazorover 3 years ago
I tried out server side events, but they are still quite troubling with the lack of headers and cookies. I remember I needed some polyfill version which gave more issues.
评论 #30313375 未加载
axiosgunnarover 3 years ago
So do I understand correctly that when using SSE, the login cookie of the user is not automatically sent with the SSE request like it is with all normal HTTP requests? And I have to redo auth somehow?
评论 #30313782 未加载
The_rationalistover 3 years ago
for bidi Rsocket is much better than wevsocket, in fact its official support is the best feature of spring boot