Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send cookies with socket.io-client

I am trying to connect to a websocket. I would like to add cookies from the actual website login, so the server (which is NOT mine), knows who I am (events are account specific).

var opts = {
   extraHeaders: {
        'Cookie': "_ga=GA1.2.17432343994.1475611967; _gat=1; __cfduid=dc232334gwdsd23434542342342342475611928"
    },
}

function socket() {
            var socket = io(websiteURL, opts);
            var patch = require('socketio-wildcard')(io.Manager); patch(socket);

            socket.on('connect', function () {
                console.log(" > [Connected]");

            });
            socket.on('*', function (data) {                
                console.log(" >>> " + data);
            });

            socket.on('disconnect', function () {
                console.log(" > [Disconnected]");
            });

}

The connection itself works fine, since I am receiving the public events from the website (not per-account ones).

I tried to find the problem using node-inspector.

This is the first request that is being done. It seems like the request headers are empty and the cookies are missing from there.

Node-Inspector: enter image description here

Normal usage of the website in chrome: http://i.imgur.com/96h0PEV.jpg

(Yes, I am sending less cookies in node, just to see if they pop up in the request cookies)

Am I doing something wrong? How would I add cookies the right way?

like image 997
Thomas Weiss Avatar asked Apr 27 '17 16:04

Thomas Weiss


People also ask

How do I send cookies from server to client?

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

Are cookies sent with WebSockets?

Although, in theory, one could use cookies, as all WebSocket connections start with an HTTP request (with an upgrade header on it), and the cookies for the domain you are connecting to, will be sent with that initial HTTP request to open the WebSocket.

What is the difference between Socket.IO and Socket.IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

Can I use Socket.IO client to connect to a standard WebSocket?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.


1 Answers

I had a similar problem, I think your options have to set the cookie for the "polling" transport method, like this:

var opts = {
    transportOptions: {
        polling: {
            extraHeaders: {
                'Cookie': '_ga=GA1.2.17432343994.1475611967; _gat=1; __cfduid=dc232334gwdsd23434542342342342475611928'
            }
        }
    }
}

I got this to work with the following all-in-one server-client example:

const express = require('express');
const http = require('http');

const app = express();
const httpServer = new http.Server(app);
const socketServer = require('socket.io')(httpServer);

const LISTEN_PORT = 4444;

socketServer.on('connection', socket => {
    const cookieString = socket.handshake.headers.cookie;
    console.log('server connection ' + (cookieString || ''));

    setInterval(() => socket.emit('ping', Date.now()), 1000);
});

let socketClient = undefined;
httpServer.listen(LISTEN_PORT, 'localhost', () => {
    console.log('web server started')

    const options = {
        transportOptions: {
            polling: {
                extraHeaders: {
                    'Cookie': 'hello=world'
                }
            }
        }
    }
    socketClient = require('socket.io-client').connect('http://localhost:'+LISTEN_PORT, options);
    socketClient.on('ping', (data) => console.log('ping ' + data));
});
like image 156
m0tive Avatar answered Oct 03 '22 23:10

m0tive