Re:Server-Sent Events: an alternative to WebSockets
by whazor on Saturday February 12, 2022 @10:02AM
(#30313301)
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.
[ Parent
| on HN ]
Re:Server-Sent Events: an alternative to WebSockets
by bullen on Saturday February 12, 2022 @10:15AM
(#30313375)
How do you mean lack of headers and cookies?
That is wrong. Edit: Actually it seems correct (a javascript problem, not SSE problem) but it's a non-problem if you use a parameter for that data instead and read it on the server.
Re:Server-Sent Events: an alternative to WebSockets
by tytho on Saturday February 12, 2022 @10:31AM
(#30313515)
You cannot send custom headers when using the built-in EventSource[1] constructor, however you can pass the ‘include’ value to the credentials option. Many polyfills allow custom headers.
However you are correct that if you’re not using JavaScript and connecting directly to the SSE endpoint via something else besides a browser client, nothing is preventing anyone from using custom headers.
[1] https://developer.mozilla.org/en-US/docs/Web/API/EventSource...
Re:Server-Sent Events: an alternative to WebSockets
by bullen on Saturday February 12, 2022 @10:35AM
(#30313539)
Aha, well why do you need to send a header when you can just put the data on the GET URL like so "blabla?cookie=erWR32" for example?
In my example I use this code:
var source = new EventSource('pull?name=one');
source.onmessage = function (event) {
document.getElementById('events').innerHTML += event.data;
};
Re:Server-Sent Events: an alternative to WebSockets
by tytho on Saturday February 12, 2022 @10:52AM
(#30313679)
I think that works great! The complaint I’ve heard is that you may need to support multiple ways to authenticate opening up more attack surface.
Re:Server-Sent Events: an alternative to WebSockets
by kreetx on Saturday February 12, 2022 @10:53AM
(#30313682)
What if you use http-only cookies?
Re:Server-Sent Events: an alternative to WebSockets
by tytho on Saturday February 12, 2022 @10:56AM
(#30313704)
You can pass a ‘withCredentials’ option.
Re:Server-Sent Events: an alternative to WebSockets
by withinboredom on Saturday February 12, 2022 @10:34AM
(#30313530)
I’m pretty sure I saw him sending headers in the talk. Did you watch the talk?
Re:Server-Sent Events: an alternative to WebSockets
by tytho on Saturday February 12, 2022 @10:50AM
(#30313663)
He was likely using a polyfill. It’s definitely not in the spec and there’s an open discussion about trying to get it added: https://github.com/whatwg/html/issues/2177