Reputation: 6710
I am using node.js and socket.io. on the client side, I wrote a cookie like this:
var socket = io.connect('http://localhost:3000');
document.cookie="foo=bar";
socket.emit('this', { is: 'test'});
and on the server side, I need to be able to read from that cookie inside a socket.io connection, something like this:
io.sockets.on('connection', function (socket) {
socket.on('this', function(reqData){
console.log(socket.handshake.headers); // there is no cookies here!
});
}
a no-framework solution is preferred, any help is appreciated. thanks.
update: in this gist you can find my complete code.
Upvotes: 0
Views: 1540
Reputation: 203494
Try setting the cookie before making the connection from the client:
document.cookie = "foo=bar";
var socket = io.connect('http://localhost:3000');
socket.emit('this', { is: 'test'});
Here is a minimal example that I tested with. The server console.log's a new cookie with each reload of the page, as expected.
Upvotes: 0