Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!