Monday, 17 April 2017

Controlling 2 socket.io connections

I'm working on an app that has 2 types of users. I would like to separate out their functionality in two socket.io files.

index.js:

var io = require('socket.io').listen(server);
require('/sockets/userType1')(io);
require('/sockets/userType2')(io);  

userType1.js:

module.exports = function (io) {
    io.on("connection", function (socket) {
        console.log("Connected to userType1");
    });
}

userType2.js

module.exports = function (io) {
    io.on("connection", function (socket) {
        console.log("Connected to userType2");
    });
}

However when I connect from the client side it connects to both (which I guess I would expect since I don't have anything distinguishing which one I am trying to connect to).

Is there a way to define the connection to only connect to one of those socket.io classes? or do I just need to rely on naming conventions?



via Woodsy

No comments:

Post a Comment