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 to set initially cookies to a websocket client #473

Closed
caub opened this issue Mar 22, 2015 · 12 comments
Closed

How to set initially cookies to a websocket client #473

caub opened this issue Mar 22, 2015 · 12 comments

Comments

@caub
Copy link

caub commented Mar 22, 2015

Looking at the doc, we can read cookies from a server, with upgradeReq, but from a client I don't see how to set cookies before making the connection

@3rd-Eden
Copy link
Member

In the node.js variant you can supply an options object with a headers key where you can add the Cookie header your self.

@caub
Copy link
Author

caub commented Mar 24, 2015

What is the node.js variant? still in ws? Could you show a code example please?

@mems
Copy link

mems commented Nov 18, 2015

I think he means in node WebSocket server you can write Set-Cookie header like that:

const wss = new WebSocketServer({
    port: 9000
});
wss.on("headers", function(headers) {
    headers["set-cookie"] = "SESSIONID=" + crypto.randomBytes(20).toString("hex");
    console.log("handshake response cookie", headers["set-cookie"]);
});
wss.on("connection", function(ws) {
    console.log("connection request cookie: ", ws.upgradeReq.headers.cookie);
});

@ghost
Copy link

ghost commented Feb 14, 2016

+1 set cookie in node js ? ws

@eugen35
Copy link

eugen35 commented Apr 26, 2016

Hello!

I need to set cookie. This decision is not work ((( ! **
**
wss.on("headers", function(headers) {
headers["set-cookie"] = "SESSIONID=" + crypto.randomBytes(20).toString("hex");
console.log(headers["set-cookie"]);
});

In console:
[ 'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
'Sec-WebSocket-Accept: vGkfnvhYceS53qvrDOjE8Clteq0=',
'Sec-WebSocket-Extensions: permessage-deflate',
'Set-Cookie': 'repa=1461679469325; domain=""; path=/' ]

But in client cookie not setted, in ws.upgradeReq.headers (on connection event) is not setted!

Is there any way to set cookie ( ?
What may be wrong?

PS: I use WSS

@mems
Copy link

mems commented Apr 26, 2016

@eugen35 You've got something else overriding the Set-Cookie header with the cookie "repa" (and a timestamp as value) before sent by the server

@eugen35
Copy link

eugen35 commented Apr 26, 2016

No its my experiments with sets of cookie. Any way cookie "repa" is felt on client and in response

@andreevWork
Copy link

andreevWork commented Jul 17, 2016

I find solution:

web_socket_server.on('headers', (headers) => {
      headers.push('Set-Cookie: ' + cookie_parser.serialize('testing', 'test')});
});

headers is a Array, not Object, because solution @mems , don`t work.
UPD: cookie_parser is a npm module: https://www.npmjs.com/package/cookie

@whiler
Copy link

whiler commented Aug 16, 2016

code example

var WebSocket = require('ws');
var cookie = require('cookie');

var ws = new WebSocket(
    'http://localhost/auth',
    [],
    {
        'headers': {
            'Cookie': cookie.serialize('id', '496E66DD')
        }
    }
);

@brenthmiras
Copy link

@whiler This worked for me when using ws as a web socket client in nodejs. Can you please provide a link to the documentation?

@kafkahw
Copy link

kafkahw commented Jun 27, 2019

@shapengw
Copy link

shapengw commented Sep 4, 2019

code example

var WebSocket = require('ws');
var cookie = require('cookie');

var ws = new WebSocket(
    'http://localhost/auth',
    [],
    {
        'headers': {
            'Cookie': cookie.serialize('id', '496E66DD')
        }
    }
);

Can I know, how to receive or get, the cookie which be set in server side?
The server side code as below

	var responseHeader = make(http.Header)
	var cookie = &http.Cookie{
		Name:   "foo",
		Value:  "bar",
		Secure: true,
	}
	responseHeader.Add("Set-Cookie", cookie.String())

What exact way, I can get this cookie, from client side
Client side code like below:

 if (window["WebSocket"]) {
      let self = this
      var wss = new WebSocket("wss://xxx.com/api/v5/wss/v3/wss")
      wss.addEventListener("open", onOpenFunc)
      function onOpenFunc(event) {
          //how to get the cookie here?
      }

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

9 participants