Search code examples
node.jssocketsiosocket.io

Socket IO doesn't send and receive cookies


My app architecture in development environment is following: Web server running on port 3000, Api server running on port 3001, Socket io server running on port 3002.

I connect to the socket io server on the client side like this:

const socket = io('http://127.0.0.1:3002/bitcoin');

And here's my connection event on the socket io server:

// www

const io = require('./controllers/socket_io');

io.listen(3002);

// controllers/socket_io.js

const io = require('socket.io')();

const Bitcoin_Socket_IO_C = require('./bitcoin/socket_io');

io.of('/bitcoin').on('connection', socket => Bitcoin_Socket_IO_C.connect(socket));

module.exports = io;

// controllers/bitcoin/socket_io.js

class Bitcoin_Socket_IO_Controller{
    static async connect(socket){
        console.log(socket);
    };
};

module.exports = Bitcoin_Socket_IO_Controller;

My io server sees the connection but never sends the io cookie back. My api can set auth cookies as well, and I want to see these in my socket io handshake, but they don't go there as well.

What's the problem here?


Solution

  • I solved it. As I was accessing my web server thorough localhost, all the cookies were set to that domain. My client's io was connection to the 127.0.0.1 though, and so no cookies were sent there.