> The quick rule: if you need bidirectional, low-latency communication (chat, collaboration, games), WebSocket; if you only push from the server, SSE is simpler and cheaper to operate.
For most apps just use SSE and the built-in code for making HTTP requests (Fetch) instead of hacking up your own client side JS to make requests over a WebSocket. The latency is the same because modern browsers multiplex HTTP requests over a single TCP connection that is left open.
Maybe if you are making many client requests per second there is an advantage to not sending full headers/cookies/etc... on each request but not if you're sending requests in response to user clicks/touches.
Any sufficiently complicated SPA contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Fetch.
> The latency is the same because modern browsers multiplex HTTP requests over a single TCP connection that is left open.
In my experience this isn’t true; firstly you’re relying on an implementation detail of the platform that you’re executing on, of which you have no control over on the client side. Secondly, even if you aren’t opening a new connection per request, you’re still travelling through an entire HTTP stack implementation rather than the incredibly simple WebSocket protocol - effectively a length and a mask to get the contents, rather than some (in http1 land) fuzzy parser.
If you can guarantee you’re hitting http2 or http3 then you might be closer in latency, but due to the complexity of both I would imagine plain http1 negotiated persistent WebSockets provide the best latency.
You're going to run into all kinds of issues with head of line blocking, and the fact that browsers typically only allow a few persistent connections to the same origin at a time.
Just do regular HTTP(2) requests. Browsers are extremely optimized for that. You don't need to invent your own transport and then manually splat a bunch of dynamic HTML on the dom.
What’s wrong with this idea, really? It’s redundant because you can serve HTML to requests with Apache or Ngnix or any other server on the happy path.
What’s right with the idea, really? It’s exactly what the tried and true preferences of developers have been shown to be: getting in the way of the happy path for no reason.
Now you can have build steps and put story points in Jira and do it all on the server where we don’t have to see it, and the success condition is that the text gets served. Both sides can be happy now.
> What’s wrong with this idea, really? It’s redundant because you can serve HTML to requests with Apache or Ngnix or any other server on the happy path.
Overhead, security, denial of service.
You now have two states to track. Is the web-server up to date with the version the web socket is hosting?
Is monitoring correct? Is your web server going to be alive if the web socket server is offline?
If they both use the same source of truth why not just use the web-server which is more tried and true and much more reliable than JavaScript.
And you can actually serve a page without requiring JavaScript. If corporate firewalls are blocking web sockets you rendering blank because there isn't any easy way to determine if the client is blocked.
You still need something to serve the JavaScript fronted of the website so unless you create a WebSocket HTTP server which you're then you're opening can of worms, you have a web-server sitting idle wasting resources.
I then bombard your web socket server and now you're rendering nothing at all yet the web server is still trying to serve content, how do you determine if the web socket is dead? More complexity.
Responsive site built via server-side render, with a particular benefit for partial page updates. 90% of the benefit of an SPA but 10% of the code: no api, no moving of system of record for state back and forth between database and browser (it's always db), etc. And you can avoid react.
A common use case: eg in Rails, a user has a table open. you can stream new records to the top of that table as they are created in ~5 lines of ruby (a broadcast on the model, and put the table rows in a turbo_frame with a turbo_stream_from somewhere on the page).
There are real limits to this -- you have to hold the update dependency graph in your head -- but the benefits are huge for small to medium amounts of responsiveness.
My experience has been great with Rails/Turbo and htmx.
> The quick rule: if you need bidirectional, low-latency communication (chat, collaboration, games), WebSocket; if you only push from the server, SSE is simpler and cheaper to operate.
For most apps just use SSE and the built-in code for making HTTP requests (Fetch) instead of hacking up your own client side JS to make requests over a WebSocket. The latency is the same because modern browsers multiplex HTTP requests over a single TCP connection that is left open.
Maybe if you are making many client requests per second there is an advantage to not sending full headers/cookies/etc... on each request but not if you're sending requests in response to user clicks/touches.
Any sufficiently complicated SPA contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Fetch.
> The latency is the same because modern browsers multiplex HTTP requests over a single TCP connection that is left open.
In my experience this isn’t true; firstly you’re relying on an implementation detail of the platform that you’re executing on, of which you have no control over on the client side. Secondly, even if you aren’t opening a new connection per request, you’re still travelling through an entire HTTP stack implementation rather than the incredibly simple WebSocket protocol - effectively a length and a mask to get the contents, rather than some (in http1 land) fuzzy parser.
If you can guarantee you’re hitting http2 or http3 then you might be closer in latency, but due to the complexity of both I would imagine plain http1 negotiated persistent WebSockets provide the best latency.
A good response to this post
https://yagni.club/3mstlyuxe5s26
Definitely a well reasoned counter point to choosing websockets over SSE.
Love how DHTML, ASP.NET Ajax, JSF Ajax kind of keeps being re-invented.
You're going to run into all kinds of issues with head of line blocking, and the fact that browsers typically only allow a few persistent connections to the same origin at a time.
Just do regular HTTP(2) requests. Browsers are extremely optimized for that. You don't need to invent your own transport and then manually splat a bunch of dynamic HTML on the dom.
What’s wrong with this idea, really? It’s redundant because you can serve HTML to requests with Apache or Ngnix or any other server on the happy path.
What’s right with the idea, really? It’s exactly what the tried and true preferences of developers have been shown to be: getting in the way of the happy path for no reason.
Now you can have build steps and put story points in Jira and do it all on the server where we don’t have to see it, and the success condition is that the text gets served. Both sides can be happy now.
> What’s wrong with this idea, really? It’s redundant because you can serve HTML to requests with Apache or Ngnix or any other server on the happy path.
Overhead, security, denial of service.
You now have two states to track. Is the web-server up to date with the version the web socket is hosting?
Is monitoring correct? Is your web server going to be alive if the web socket server is offline?
If they both use the same source of truth why not just use the web-server which is more tried and true and much more reliable than JavaScript.
And you can actually serve a page without requiring JavaScript. If corporate firewalls are blocking web sockets you rendering blank because there isn't any easy way to determine if the client is blocked.
You still need something to serve the JavaScript fronted of the website so unless you create a WebSocket HTTP server which you're then you're opening can of worms, you have a web-server sitting idle wasting resources.
I then bombard your web socket server and now you're rendering nothing at all yet the web server is still trying to serve content, how do you determine if the web socket is dead? More complexity.
Is this satire? Rage bait? Why would you ever do this?
Just make a normal website!! You've invented an MPA with extra steps!
Responsive site built via server-side render, with a particular benefit for partial page updates. 90% of the benefit of an SPA but 10% of the code: no api, no moving of system of record for state back and forth between database and browser (it's always db), etc. And you can avoid react.
A common use case: eg in Rails, a user has a table open. you can stream new records to the top of that table as they are created in ~5 lines of ruby (a broadcast on the model, and put the table rows in a turbo_frame with a turbo_stream_from somewhere on the page).
There are real limits to this -- you have to hold the update dependency graph in your head -- but the benefits are huge for small to medium amounts of responsiveness.
My experience has been great with Rails/Turbo and htmx.
> Simple, elegant and fast.
Until someone bombs your websocket server and you then have nothing at all.
great one