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?
Socket.io NAMESPACES
userType1.js:
module.exports = function (io) {
var nsp = io.of('/userType1');
nsp.on("connection", function (socket) {
console.log("Connected to userType1");
});
}
userType2.js
module.exports = function (io) {
var nsp = io.of('/userType2');
nsp.on("connection", function (socket) {
console.log("Connected to userType2");
});
}
userTypeALLUSERS:
module.exports = function (io) {
io.on("connection", function (socket) {
console.log("Connected to ALL USERS");
});
}
Client Side (for usertype2):
var socket = io('/userType2');
server console:
Connected to userType2
Connected to ALL USERS