Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:16:40+00:00 2026-05-28T06:16:40+00:00

I am trying to write a multi channel application in socket.io. The channel you

  • 0

I am trying to write a multi channel application in socket.io. The channel you are in should be defined by the url you are on. If I do the joining part in the app.js with permanent values everything works. As soon as I change it so that the route for route.page does the joining I get the error, that sockets is not available in the context. What would be the correct way so that I can dynamically join the channel?

/app.js

var io = socketio.listen(app);
require('./io')(io);

io.sockets.on('connection', function (socket) {  
  socket.on('debug', function (message) {
    socket.get('channel', function (err, name) {
      socket.in(name).broadcast.emit('debug', message);
    });
  });
});

/io.js

var socketio = function (io) { 
  if (!io) return socketio._io;  
  socketio._io = io;
} 

module.exports = socketio;

/routes/index.js

var io = require('../io')();
exports.page = function(req, res){
  var channel = req.params.id;
  res.render('page', { title: 'PAGE', channel: channel  });
  io.sockets.on('connection', function (socket) {
    socket.join(channel);
    socket.set('channel', channel );
  });
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:16:41+00:00Added an answer on May 28, 2026 at 6:16 am

    The easiest way I’ve found to do multiple channels is off of different URLs.

    For example I have the client do the following:

    io.connect('/game/1')
    io.connect('/system')
    

    and on the server I have

    io.of('/game/1').on('connect' function(socket) {...})
    io.of('/system').on('connect' function(socket) {...})
    

    It looks like I’m connecting twice here, but socket.io is smart enough to use a single websocket for this connection (at least it says so in the how-to-use).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write my first true multi layered application and I was
I'm trying to write a multi-file patch for an open-source project, but the master
im trying to write an app that will display a list off lines from
I'm trying to write a Java method to preform a multi-pop off a stack.
I'm trying to write an editor overtop a multi-threaded game engine. In theory, through
i have created a multi-module maven project and i am trying to write and
I am trying to build a multi-app CodeIgniter site, where the assets for all
I'm trying to write a multi-threaded program, the number of threads based on command-line
I am trying to write a WinForms app, that allows searching for a files,
I am writing a multi threaded application and I am also trying to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.