socket emitting beyond the room

Asked

Viewed 54 times

0

All clients are receiving the message via socket.io.
I would like only users who are in the room receive.

Server.js

sockets.on('connection', function (action) {
   action.join('testroom');

   action.on('say to someone', function(msg){
      sockets.to('testroom').emit('some event', msg);
   });

});

Client in which the message is sent:

Client A

socket.on('connect', function () {
   socket.emit('join', 'testroom');
});

socket.on('some event', function (data) {
    console.log(data);
});

$('form').submit(function () {
   event.preventDefault();
   socket.emit('say to someone', $('#m').val() );
   $('#m').val('');
});

Client who is receiving the message improperly:

Client B

socket.on('connect',(socket) => {
    socket.on('some event', data => {
      console.log(data);
    });

}); 

Ps: Client B is being used in Vuejs.

1 answer

0

Friend I’m on the street now but I believe your problem is

sockets.on('connection', function (action) {
   action.join('testroom');

   action.on('say to someone', function(msg){

    //essa parte aqui 
      sockets.to('testroom').emit('some event', msg);


      //Mude para isso// onde o 'io' é a variavel onde voce armazenou o socket.io // Exemplo : var io = require('socket.io')(http);
      
      io.sockets.in('testroom').emit('some event', msg);
      
   });

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.