Thursday 4 May 2017

Socket io not broadcasting to specific room

I'm trying to broadcast some info to a specific room:

server side

var io = require('socket.io')(server, { path: '/my/path' });
server.listen(8090, function () {
    console.log("Server ON.");
});

io.on('connection', function(socket){
    socket.on('room', function(room) { //set the room
        socket.join(room);
        socket.room = room;
    });
    socket.on('new message', function (data) { //receive and broadcast the msg
        socket.broadcast.to(socket.room).emit('new message', data);
    });
});

client side

//initial client config
var socket = io(URL_SOCKET, {path: '/my/path'});
socket.emit('room', room_id);
socket.on('new message', function (data) {
    do_something(data);
});

function on_change(data){
    socket.emit('new message', data);
}

The problem here is: the socket.broadcast.to is broadcasting the information sent by clients (by calling on_change) to all the rooms (to everyone). What am I doing wrong?



via Mr. Felix

No comments:

Post a Comment