SSE doesn’t support Authorization headers, which made it DOA for my purposes. What a pity - it would’ve been a perfect fit for job statuses, progress of processing, etc
Do Websockets support Authorization headers? I've been trying to figure that out in a spring-boot server for the last couple of days without reaching anything. Any helpful links would be great.
Agreed, but that should be less of an issue if you use a stateless token solution. Plenty of web API providers still exist which have you pass your token in the query params (though perhaps "other people do it" is a bad argument for why it's ok).
To workaround this generally I pass a JWT with expiration built in every time a socket is opened. If the JWT expired or is not authorized then they get dropped.
You’ll literally have to either encode your auth info into the websocket path, or create a mechanism for generating a one-time websocket URL from something that does respect Authorization, or implement a phase after connecting via the websocket that demands authorization before doing anything else in the bidirectional channel.
You do need that header if you’re streaming pushes to the client from a protected resource, like say from an API to drive a web app and you want consistency (Authorization: Bearer ...).
Ah, so the issue is the spec'd interface is missing support for custom headers.
Do you know whether it just missed / not considered (in which case it could be added), or it was omitted for specific reasons?
edit: I guess you could use a separate endpoint for auth and rely on session cookies though, assuming these are properly sent.
edit 2: https://github.com/whatwg/html/issues/2177 apparently the reasoning is "it's easy enough to implement SSE over fetch", which is a bit… shitty, especially given the edge services (e.g. reconnection configurable via stream messages). Apparently some of the "polyfills" extend the interface with headers support and the like. Still, would be nice if (as suggested by some comments) EventSource accepted a Request object as alternative to a plain endpoint.
You can see that “mimicking SSE with fetch” is no solution at all, considering that inherits all the weaknesses of XHR long polling with none of the benefits.
You can see that “mimicking SSE with fetch” is no solution at all
It solves your issue that SSE doesn't support custom headers.
that inherits all the weaknesses of XHR long polling
It doesn't: fetch supports streaming bodies, so you can keep the connection open and convert incoming segments to events / messages as they arrive, meaning neither the server costs (of tearing down and re-establishing the connection after every event) nor the ordering issues are concerns.
Firefox did not support ReadableStream being accessible to fetch (behind a feature flag until recently)
Fallbacks to XHR broke because browsers need around 2kb of data to pass to onProgress function. So pad your data up to 2kb to ensure it gets to JS timely
Partial message and needing to reassemble half messages, split multimessages because sometimes Chrome gave me a bunch at a time
Granted, SSE would have only fixed the last two points, but those are important points!
Honestly, I don’t think you even bothered to try to solve a similar problem that I have tried.
Because you wouldn’t have wasted my time with things I already tried, then ended it with a condescending “standard HTTP” remark.
10
u/[deleted] Jun 13 '19
SSE doesn’t support
Authorizationheaders, which made it DOA for my purposes. What a pity - it would’ve been a perfect fit for job statuses, progress of processing, etc