Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How else can I set custom headers? #467

Closed
binarykitchen opened this issue Mar 9, 2015 · 14 comments
Closed

How else can I set custom headers? #467

binarykitchen opened this issue Mar 9, 2015 · 14 comments

Comments

@binarykitchen
Copy link

I know there were heavy discussions in #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?

@3rd-Eden
Copy link
Member

3rd-Eden commented Mar 9, 2015

Why would the browser code be the problem here? Browsers don't even support custom headers.

@binarykitchen
Copy link
Author

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.

@3rd-Eden
Copy link
Member

3rd-Eden commented Mar 9, 2015

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.

@binarykitchen
Copy link
Author

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.

@3rd-Eden
Copy link
Member

3rd-Eden commented Mar 9, 2015

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)

@binarykitchen
Copy link
Author

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 ...

@3rd-Eden
Copy link
Member

3rd-Eden commented Mar 9, 2015

Correct. See https://github.com/websockets/ws/blob/master/doc/ws.md#websocketupgradereq

@binarykitchen
Copy link
Author

Yes, it worked. Thanks so much man!

@irshad-qb
Copy link

@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 ?

@streamnsight
Copy link

@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?

@iostreamer-X
Copy link

iostreamer-X commented Jul 4, 2016

@streamnsight
You want something like this?

WebSocket = require 'ws'
ws = new WebSocket 'ws://localhost:8000',{
        headers : {
          token: getToken()
        }
      }

@streamnsight
Copy link

@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.

@iostreamer-X
Copy link

I am sorry I think I misunderstood the problem

@pranavwani
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants