title: socket.io - Client connects to two sockets by same server
body: <p>I am trying to build socket connections between React client-side and Node.js server-side. But the server will host two sockets. Here is the server-side code</p>
  
  <pre><code>var app = express();
  var server = http.createServer(app);
  var io = require('socket.io')(2893, {
      path: "/ws",
      resource: "/ws",
      transports: ['websocket'],
      pingTimeout: 5000
  });
  var redis = require('redis');
  const subscriber = redis.createClient();
  require('./server/route')(app, io);
  require('./server/lib/subscriber')(require('socket.io').listen(server), subscriber);
  </code></pre>
  
  <p>The first socket connection is ok, but I wonder why the second one is not working (which is attached with <code>listen(server)</code>. Here is <code>subscriber</code> module I wrote:</p>
  
  <pre><code>module.exports = (io, subscriber) =&gt; {
  
      io.sockets.on('connection', (socket) =&gt; {
          console.log(socket);
          socket.on('room', (room) =&gt; {
              socket.join(room);
          });
      });
  
      subscriber.on('pmessage', (pattern, channel, message) =&gt; {
          const msg = JSON.parse(message);
          const idCallcenter = msg.idCallcenter;
          return io.to(idCallcenter).emit('message', { type: channel, message: msg });
      });
  
      subscriber.psubscribe('*');
  
  };
  </code></pre>
  
  <p>And the client-side React module</p>
  
  <pre><code>var socketOption = { path: "/ws", transports: ['websocket'] };
  var socket = io("http://localhost:2893", socketOption);
  var socket2 = io.connect("http://localhost:4004");
  export default function (user) {
    debugger
    socket.user = user;
    contact(socket);
    notify(socket);
    socket.on('connect', function () {
      debug('socket connect', socket.id);
      store.dispatch(connectNetworkSuccess());
      socket.emit('user-online', {
        idUser: user._id,
        idCallcenter: user.idCallcenter,
        email: user.email
      });
    });
    socket2.on('connect', () =&gt; {
      debug('Socket connected');
      socket2.emit('room', user.idCallcenter);
  
    });
    socket2.on('message', (data) =&gt; {
      debugger
      debug('Socket message');
      debug(data);
      const type = data.type;
      const message = data.message;
      if (type === 'recordFetched') {
      }
  
    });
    socket.emit('user-online', {
      idUser: user._id,
      idCallcenter: user.idCallcenter,
      email: user.email
    });
    socket.on('disconnect', function (reason) {
      debug('socket disconnect', reason);
      store.dispatch(connectNetworkFailed());
    });
  } 
  </code></pre>
  
  <p>The first <code>socket</code> (in port 2893) runs normally. Meanwhile, <code>socket2</code> (in port 4004) does not connect. It does not jump into <code>connection</code> callback of both server and client sides. What did I do wrong here?</p>