News for hackers. Stuff that matters.
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by Fabricio20 on Monday March 18, 2024 @02:05PM (#39747798)
To this day I still dont know why WebSockets and SSE dont support sending headers on the initial request, such as Authorization. Leaving authentication on realtime services entirely up to whoever is implementing the service.

I may be wrong here and the spec suggests a good way to do it, but i've seen so many different approaches that at this point might as well say there's none.

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by michaelt on Monday March 18, 2024 @02:15PM (#39747916)
> To this day I still dont know why WebSockets and SSE dont support sending headers on the initial request

Doesn't the initial request get to send a full set of standard HTTP headers, cookies and all?

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by paulgb on Monday March 18, 2024 @02:49PM (#39748348)
It does, but if you're calling it from the browser you can't add arbitrary data to them (the way you can in e.g. a `fetch`)
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by makkesk8 on Monday March 18, 2024 @04:21PM (#39749498)
Someone at Azure thought of this[1]

[1] https://github.com/Azure/fetch-event-source

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by pier25 on Monday March 18, 2024 @06:43PM (#39750940)
> you can't add arbitrary data to them

What about intercepting the request with a service worker?

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by cbsmith on Monday March 18, 2024 @02:34PM (#39748156)
There's always TLS certificates... ;-)
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by dist-epoch on Monday March 18, 2024 @02:30PM (#39748085)
They do send cookies.
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by Sammi on Tuesday March 19, 2024 @05:00AM (#39753953)
I do login the bog standard way with a regular old http request and the server responds with setting an http only cookie. Then I reconnect the websocket, which will then provide the cookie to the server on reconnect.
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by omgtehlion on Monday March 18, 2024 @02:15PM (#39747914)
Wait, what?? Been using these for years. Am I missing something?
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by tytho on Monday March 18, 2024 @02:31PM (#39748101)
The browser EventSource constructor does not have options to pass in your own headers. You can pass an option to have it use the cookies for the domain you’re using. There are libraries that allow you to pass in additional HTTP options, but they essentially reimplement the built-in EventSource object in order to do so. Not terribly difficult, fairly simple spec.
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by omgtehlion on Monday March 18, 2024 @02:46PM (#39748306)
Well, that constructor by default sends all the headers you have for your own domain and auth you are entitled to. This is how all other APIs in browsers work due to security and privacy concerns.

If you call to other domains, then this problem is no different to what we had with CORS years ago.

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by apitman on Monday March 18, 2024 @04:42PM (#39749756)
> This is how all other APIs in browsers work due to security and privacy concerns

They're probably comparing it to the fetch and XHR APIs, which both allow custom headers.

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by apitman on Monday March 18, 2024 @02:30PM (#39748089)
They're probably referring to browsers specifically. The WebSocket constructor doesn't allow for headers
Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by paol on Monday March 18, 2024 @02:41PM (#39748249)
Oooh boy you touched a pet peeve. I mean who needs authentication on the modern Web right? /s

The even more irritating thing is that there is nothing preventing this, and every server I've tried supports it. It's only the browser WebSocket API that was designed without this. Cookies are the only thing browsers will deign to send in the initial request.

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by rexxars on Monday March 18, 2024 @07:38PM (#39751382)
The EventSource API (the browser "client API" for Server-Sent Events) leaves a lot to be desired. While I am a maintainer of the most used EventSource polyfill[1], I've recently started a new project that aims to be a modern take on what an EventSource client could be: https://github.com/rexxars/eventsource-client.

Beyond handling the custom headers aspect, it also supports any request method (POST, PATCH..), allows you to include a request body, allows subscribing to any named event (the EventSource `onmessage` vs `on('named event')` is very confusing), as well as setting an initial last event ID (which can be helpful when restoring state after a reload or similar). And you can use it as an async iterator.

I love the simplicity of Server-Sent Events, but the `EventSource` API seem to me like a rushed implementation that just kinda stuck around.

[1]: https://github.com/eventsource/eventsource

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by martypitt on Tuesday March 19, 2024 @02:07AM (#39753348)
Nice work! This addresses many of the issues I've had with SSE.

Another problem we've never worked out the solution to, is how to send a termination - signalling "there are no more events coming". We always end up having to roll our own, though it felt like something that should've been handled at the protocol layer.

Re:WebSockets vs. Server-Sent-Events vs. Long-Polling vs. We... by skinner927 on Tuesday March 19, 2024 @02:36AM (#39753438)
Just read the spec

Clients will reconnect if the connection is closed; a client can be told to stop reconnecting using the HTTP 204 No Content response code.