question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

socket.join not working

See original GitHub issue

Repost from SO, but I need answers so here:

So, my socket.join calls are doing nothing. After calling socket.join, and then listing the rooms the socket is in, the socket is only connected to the room defined by it’s id (it’s own room).

    function onJoin(room) {
        console.log("Joining room: " + room);
        socket.join(room);
        console.log(socket.id + " now in rooms ", socket.rooms);
    }

Would print for example:

    > Joining room: 5aba92759b9ffa9fdf579714d6aa125ddb05cb1172611331775e7a69dab37258
    > Q6D4h17DvdOZrbrEAAAC now in rooms  { Q6D4h17DvdOZrbrEAAAC: 'Q6D4h17DvdOZrbrEAAAC' }

If it makes a difference, here’s how my socket server is being created:

    //app.js
    var app = express();
    var http = require('http');
    var server = http.Server(app);
    var io = require('socket.io')(server);
    var chat = require('./routes/chat/chat')(io);

    //chat.js
    module.exports = function(io) {
      io.sockets.on('connection', function(socket) {
        
        socket.on('join', onJoin);
        ...
    }

Where’s the issue?

  • Socket.io v1.7.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
darrachequesnecommented, Apr 20, 2017

The join method is asynchronous:

socket.join(room, function () {
  console.log(socket.id + " now in rooms ", socket.rooms);
});
6reactions
OzairPcommented, Jun 5, 2018

@chuckdries welcome to the world of socket.io documentation

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.join does not work (socket.io) - Stack Overflow
The issue was that socket.join is async. So this would work as expected: socket.join(room, function() { console.log("Socket now in rooms", ...
Read more >
Rooms | Socket.IO
A room is an arbitrary channel that sockets can join and leave. It can be used to broadcast events to a subset of...
Read more >
Socket.IO - Rooms - Tutorialspoint
Joining Rooms. You can call the join method on the socket to subscribe the socket to a given channel/room. For example, let us...
Read more >
Chat Rooms With Socket.io - Medium
You've got yourself a basic express app running on localhost:5000. ... The only difference is socket.in will not include the sender, ...
Read more >
Socket = io join multiple rooms, socket.broadcast.emit not working ...
Socket.io join another client to a room, listen for a custom event from the ... Then the problem is the same: socket.broadcast.emit is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found