I know there were heavy discussions in https://github.com/websockets/ws/issues/227 but it is not clear to me what the current state is.
All I need is the ability to set custom headers for an API I am working on. Without this functionality everything would become a lot more complicated. If I look at https://github.com/websockets/ws/blob/master/lib/WebSocket.js#L49, I can see that options are already supported.
The problem obviously lies here
https://github.com/websockets/ws/blob/master/lib/browser.js#L33
Anyone?
Why would the browser code be the problem here? Browsers don't even support custom headers.
Hmmm, maybe I am just confused. Initially I thought the new WebSocket(...) line on https://github.com/websockets/ws/blob/master/lib/browser.js#L33 is calling the constructor at https://github.com/websockets/ws/blob/master/lib/WebSocket.js#L46 which has options for the third argument where you could set custom headers.
I guess, one is referring to the native browser code, the other one is like a mockup. Or am I interpreting this the wrong way?
Ultimately I am after custom headers between client and server.
If your "client" is actually a node script which uses the ws's client interface to create a connection then custom headers are possible. If you are referring to client as an actual browser then it's not possible as websockets cannot have custom headers. The best bet you got is to use querystrings to send data from client to server.
Yes, I am referring to browsers as clients. I see, I must have misinterpreted that code before. Sorry.
So, how can I transfer data from the browser to the client during websocket initialisation? If custom headers are out of questions, what else is possible? Query strings? I cannot picture how query strings here because these are for GET requests only. But not for web sockets. Correct me if I am wrong.
Query strings will work fine for WebSocket connections as upgrade requests contain the full URL path. On the server you can do something like console.log(url.parse(client.upgradeReq.url, true).query)
Ah, you mean we can amend query parameters to the URL we are passing onto the WebSocket constructor?
On the client something like
new WebSocket('http://myserver.com/websocket/api/xxx?apiKey=blahblah')
and on the server side an I can parse this with
url.parse(client.upgradeReq.url, true).query?
That would be great! Will check this out tonight ...
Yes, it worked. Thanks so much man!
@3rd-Eden :
"If your "client" is actually a node script which uses the ws's client interface to create a connection then custom headers are possible"
In this case how can I add a custom header in Oauth2 format ( 'Authorization' : Bearer #token) in case of node script which uses ws ?
@3rd-Eden @irshad-qb
same question here:
I want to send a session cookie to verify the client.
I managed to get the session info on the server side, but without being able to pass the cookie from the client(script) it is useless.
any hint on how to do this?
@streamnsight
You want something like this?
WebSocket = require 'ws'
ws = new WebSocket 'ws://localhost:8000',{
headers : {
token: getToken()
}
}
@iostreamer-X
Not sure I understand what you are proposing here:
I know how to pass my token to the server side. That seems to be what your client side code attempts to do.
on the server side I can validate the token, but I would like to then set a cookie on the RESPONSE from the server. i do not have access to the response object in the verifyClient function, only the request.
The only place to set a cookie on the response is in the on('headers'...) event, where I don't get access to the request or the response, just the headers, so i have nowhere to store the session ID to get it and send it in the on('headers'...) event.
I am sorry I think I misunderstood the problem
Correct. See https://github.com/websockets/ws/blob/master/doc/ws.md#websocketupgradereq
This work perfectly
Most helpful comment
@streamnsight
You want something like this?