Websocket with Authorization header
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
Hi folks,
when I establish a websocket connection the first request seems to be a http connection with a "Upgrade" header.
Further the endpoint is protected by a bearer token which needs to be added to the "Authorization" header at the http upgrade call.
After that no authentication is needed anymore while websocket connection.
Now I've the problem that I don't find the possibility to add a header to the first websocket request (client side).
Can you help me doing this?
T
when I establish a websocket connection the first request seems to be a http connection with a "Upgrade" header.
Further the endpoint is protected by a bearer token which needs to be added to the "Authorization" header at the http upgrade call.
After that no authentication is needed anymore while websocket connection.
Now I've the problem that I don't find the possibility to add a header to the first websocket request (client side).
Can you help me doing this?
T
20 Replies
Sun bearOP
How? With some libraries
I use next-ws with nextauth and I just use getSession on the incoming request in the websocket route
Sun bearOP
The websocket server is external. Its not nextjs server.
It assumes a Authorization header is the initial http call
It assumes a Authorization header is the initial http call
mhm
Sun bearOP
example:
Knopper gall
You can either use a websocket library, or use Bun.
Bun supports headers by default in it's WebSocket API
Bun supports headers by default in it's WebSocket API
Sun bearOP
Does Bun works client side?
@Sun bear Does Bun works client side?
Knopper gall
Bun is a runtime...
It replaces node
If you use Bun you'll need to install the types too, because they aren't automatically included in a nextjs app initialised with node
bun add -d @types/bunSun bearOP
hmm, to change the whole runtime of my local project just because a websocket client seems not be a good solution
@Sun bear hmm, to change the whole runtime of my local project just because a websocket client seems not be a good solution
Knopper gall
I mean it's a LOT faster than node, both as a package manager and as a runtime.
But if you stick with node, you can always use a WS library as I said before 🙂
But if you stick with node, you can always use a WS library as I said before 🙂
Sun bearOP
„A websocket library“ is not a good description. I already searched and didn‘t find a good one…
Giant panda
bun already has ws support
@Giant panda bun already has ws support
Knopper gall
Already mentioned that ^.^
@Sun bear „A websocket library“ is not a good description. I already searched and didn‘t find a good one…
Knopper gall
I already pointed you to Bun's polyfill, if you don't want to use it, feel free to explore 🙂
@Knopper gall I already pointed you to Bun's polyfill, if you don't want to use it, feel free to explore 🙂
Sun bearOP
Sorry but I don't get it. You send me a link to the bun websocket documentation.
There it mentioned how to setup a websocket server with bun. It has a section about a websocket client, too:
https://bun.sh/docs/api/websockets#connect-to-a-websocket-server
But this seems to be the usual 0815 websocket client which is used everywhere. There is a part about custom headers but there is additionally the information "This will not work in browsers.".
It does not looks like a custom bun browser implementation for a websocket client which allows to connect to a foreign websocket server...
There it mentioned how to setup a websocket server with bun. It has a section about a websocket client, too:
https://bun.sh/docs/api/websockets#connect-to-a-websocket-server
But this seems to be the usual 0815 websocket client which is used everywhere. There is a part about custom headers but there is additionally the information "This will not work in browsers.".
It does not looks like a custom bun browser implementation for a websocket client which allows to connect to a foreign websocket server...