News for hackers. Stuff that matters.
Re:You might not need WebSockets by notpushkin on Friday April 11, 2025 @07:34PM (#43659858)
> Bonus: Making it easy with eventkit

Why not just use SSE? https://developer.mozilla.org/en-US/docs/Web/API/Server-sent...

Re:You might not need WebSockets by hntrl on Friday April 11, 2025 @07:53PM (#43659988)
I've noticed some weird behaviors with the EventSource impl that browsers ship with. Chief among them being the default behavior is to infinitely reconnect after the server closes the stream, so you have to coordinate some kind of special stop event to stop the client from reconnecting. You wouldn't have that problem with the stream object from Response.body

The SSE protocol is actually just a long-running stream like I mentioned but with specific formatting for each chunk (id, event, and data fields)

as a side note, eventkit actually exports utilities to support SSE both on client and server. The reason you'd want to use eventkit in either case is because it ships with some extra transformation and observability goodies. https://hntrl.github.io/eventkit/guide/examples/http-streami...

Re:You might not need WebSockets by extheat on Friday April 11, 2025 @11:07PM (#43660986)
The reconnect thing is actually quite helpful for mobile use cases. Say the user switches the tab, closes their browser or loses network and then they return. Since SSE is stateless from the client's perspective, the client can just reconnect and continue receiving messages. Whereas with WS there's handshakes to worry about--and also other annoyances like what to do with pending requests before connection was lose.
Re:You might not need WebSockets by osigurdson on Friday April 11, 2025 @08:17PM (#43660118)
Based on my read, this basically is SSE but doesn't use the same protocol.
Re:You might not need WebSockets by jongjong on Friday April 11, 2025 @08:16PM (#43660111)
I don't know why people keep trying desperately to avoid the simplicity and flexibility of WebSockets.

A lot of times, what people need is a bidirectional connection yet somehow they convince themselves that SSE (or sometimes even long polling is better for the job)... But they end up with two different types of streams; HTTP for writes and responses and SSE for passively consuming real-time data... Two different stream types with different lifecycles; one connection could fail while the other is fine... There is no way to correctly identify what is the current connection status of the app because there are multiple statuses and data comes from multiple streams.

Re:You might not need WebSockets by hntrl on Friday April 11, 2025 @08:38PM (#43660241)
who's to say your data is coming from multiple streams? You can propagate any updates you need to make in the application to a single stream (like SSE or a long-lived response) in place of a WebSocket. your http responses can just be always 204 if all they're doing is handling updates and pushing events to aforementioned single stream.

https://en.wikipedia.org/wiki/Command%E2%80%93query_separati...

Re:You might not need WebSockets by koakuma-chan on Friday April 11, 2025 @09:02PM (#43660349)
SSE does not require a separate connection, unlike WebSockets.
Re:You might not need WebSockets by kaoD on Friday April 11, 2025 @10:04PM (#43660671)
MDN disagrees. See the huge red warning here https://developer.mozilla.org/en-US/docs/Web/API/EventSource

Unless you mean on HTTP2? But aren't WS connections also multiplexed over HTTP2 in that case?

Re:You might not need WebSockets by koakuma-chan on Friday April 11, 2025 @10:12PM (#43660707)
> MDN disagrees. See the huge red warning here https://developer.mozilla.org/en-US/docs/Web/API/EventSource

It should say "When used over HTTP/1" instead of "When not used over HTTP/2" because nowadays we also have HTTP/3, and browsers barely even use HTTP/1, so I would say it's pretty safe to ignore that warning.

> Unless you mean on HTTP2?

Any version of HTTP that supports multiplexing.

> But aren't WS connections also multiplexed over HTTP2 in that case?

There is RFC 8441 but I don't think it's actually implemented in the browsers.

Re:You might not need WebSockets by kaoD on Friday April 11, 2025 @10:38PM (#43660839)
> There is RFC 8441 but I don't think it's actually implemented in the browsers.

Found this: https://github.com/mattermost/mattermost/issues/30285

Re:You might not need WebSockets by koakuma-chan on Friday April 11, 2025 @11:32PM (#43661101)
https://chromestatus.com/feature/6251293127475200

It looks like it's supported in Chrome and Firefox but not in Safari.

Re:You might not need WebSockets by gorjusborg on Friday April 11, 2025 @10:34PM (#43660800)
I don't know why everyone... proceeds to use their own experience as proof of what everyone needs.

These are tools, not religions.

Websockets have some real downsides if you don't need bidirectional comms.

Re:You might not need WebSockets by supahfly_remix on Friday April 11, 2025 @07:43PM (#43659914)
Do CDN, such as Cloudflare, support SSE? The last time I looked, they didn't, but maybe things have changed.
Re:You might not need WebSockets by hntrl on Friday April 11, 2025 @07:57PM (#43660019)
I have a demo of this for CF workers https://github.com/hntrl/eventkit/tree/main/examples/workers...

(it's not SSE in particular, but it demonstrates that you can have a long running stream like SSE)

Re:You might not need WebSockets by bastawhiz on Friday April 11, 2025 @07:51PM (#43659976)
Yes, they are
Re:You might not need WebSockets by nyrikki on Friday April 11, 2025 @08:25PM (#43660166)
Cloudflare doesn't officially support SSE, but if you send keepalives events every 15 or 20sec or so you can reliably use SSE for 40 min + in my experiance.

No server traffic for 100+ sec officially results in a 524, so you could possibly make that keepalive interval longer, but I haven't tested it.

Make sure to have the new style cache rule with Bypass cache selected and absolutely make sure you are using HTTP/2 all the way to the origin.

The 6 connections per browser limit of HTTP/1.1 SSE was painful, and I am pretty sure auto negotiation breaks, often in unexpected ways with a HTTP/1.1 origin.

Re:You might not need WebSockets by threatofrain on Friday April 11, 2025 @09:01PM (#43660343)
On top of the comments below about SSE, I'd also point out that Cloudflare is doing some interesting stuff around serverless resumable websockets. They also have stuff for WebRTC.
Re:You might not need WebSockets by kordlessagain on Friday April 11, 2025 @07:43PM (#43659911)
SSE is the way to roll.
Re:You might not need WebSockets by gorjusborg on Friday April 11, 2025 @10:31PM (#43660783)
The problem selects the solution.

That said, I like SSE for unidirectional string-encoded events.

Re:You might not need WebSockets by tbeseda on Friday April 11, 2025 @08:48PM (#43660285)
SSE is great. Most things with websockets would be fine with SSE.

Also I don't see it being much easier here than a few primitives and learning about generator functions if you haven't had experience with them. I appreciate the helper, but the API is pretty reasonable as-is IMO

Re:You might not need WebSockets by notpushkin on Friday April 11, 2025 @11:52PM (#43661195)
I’m experimenting with SSE for realtime project deployment logs in https://lunni.dev/ and it’s been extremely pleasant so far.

The only problem is, if you want to customize the request (e.g. send a POST or add a header),you have to use a third-party implementation (e.g. one from Microsoft [1]), but I hope this can be fixed in the standards later.

[1]: https://www.npmjs.com/package/@microsoft/fetch-event-source

Re:You might not need WebSockets by hntrl on Friday April 11, 2025 @10:37PM (#43660826)
The helper example was a sore attempt to plug the project I've been working on (tinkering with it is how I came up with the example). The library I plugged has much more to do with enabling a more flexible reactive programming model in js, but just so happens to plug into the stream API pretty handily. Still an interesting look IMO if you're into that kind of stuff
Re:You might not need WebSockets by notpushkin on Saturday April 12, 2025 @12:05AM (#43661271)
No worries, I know how it feels! (I said, plugging away my own project in a sibling comment, lol)

I do like the reactive approach (in fact, I’ve reinvented something similar over SSE). I feel a standards-based solution is just ever so slightly more robust/universal.

Re:You might not need WebSockets by apitman on Friday April 11, 2025 @09:40PM (#43660565)
SSE doesn't support binary data without encoding to something base64 first. These days I'd recommend a fetch stream with TLV messages first, followed by WebSocket.
Re:You might not need WebSockets by shadowangel on Friday April 11, 2025 @11:11PM (#43661003)
It's javascript, anything simple needs a framework.