btford / angular-socket-io

Socket.IO component for AngularJS
1.51k stars 233 forks source link

Not receiving broadcasts that are .to("roomId"). #125

Closed ajbraus closed 9 years ago

ajbraus commented 9 years ago

I am trying to have various rooms following the socket.io docs, but I cannot get the client to receive the broadcasted return. I've tried with .forward() and just Socket.on(). Here is the Socket.on() example of a user joining a room, the server broadcasts the number of clients attached to the room, but the client never hears the response:

// client

Socket.emit("publish.join_room", { roomName: $routeParams.roomName });

Socket.on('broadcast.join_room', function (event, clientsCount) {
  console.log(clientsCount)
})
// server

socket.on('publish.join_room', function (data) {
    socket.join(data.roomName);
    var room = io.sockets.adapter.rooms[data.roomName]; 
    var clientsCount = Object.keys(room).length;

    socket.in(data.roomName).emit('broadcast.join_room', clientsCount)
})
ajbraus commented 9 years ago

Found this answer in SO that showed how it worked:

http://stackoverflow.com/a/14849342/882873

had to do:

// server
io.sockets.in(data.roomName).emit('broadcast.join_room', clientsCount)