Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

746
Visualizações
socket.io: send message to specific room (client side)

In socket.io, you usually use a specific syntax on the server side if you want to send a message to a specific room: io.to(room).emit('event', 'message');.

But how would a client (what I mean is the socket.io-related code running in a browser) indicate that a message should go to a specific room?

Is it common to just create something like this (of course the server has to evaluate it):

socket.emit('chat message', {room: 'abc', msg: 'hello there'});

Or does the socket.io client-side library offer a specific syntax for this purpose as well?

edit: to clarify, my suggestion from above seems to work, I'm just not sure if there's a better solution.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You've pretty much figured it out. Only the server can emit to specific rooms, because it is only the server that is actually connected to multiple clients. In other words, clients aren't connected to each other — the client-side library only manages communications between that one client and the server. So, if you want your client app to instruct the server to emit a message to all clients connected to a given room… that's basic API design. You could emit a socket event, or just do a regular http call to a route on your server. Either way you'll be sending metadata about which room(s) the server should broadcast to. The former (emitting) is probably preferred however, because that way on the server side you can use socket.broadcast.to to emit to all clients in the room EXCEPT the socket that initiated the call (a pretty common use case).

over 4 years ago · Santiago Trujillo Relatório

0

Alapeno's solution was for the client to send a specific room as part of the message data to the server, so that the server would know which room to broadcast the message to.

However, if you just want the client to send a message to its own room, you can have the server detect the client's room on its own via the socket.rooms object. Thus, you wouldn't need the client to send the name of its room as part of the message as you indicated.

Client.js:

socket.emit('chat message', 'hello');

Server.js:

socket.on('chat message', function(msg){
    var keys = Object.keys(socket.rooms);
    for (var i = 0; i < keys.length; i++) {
        io.to(socket.rooms[keys[i]]).emit('chat message', msg);
    }
});

Update: The socket.rooms object includes a sort of hash such as the example below, thus you'd better send to all the rooms in the socket.rooms to be sure the room members receive it. You can't be guaranteed the ordering of the keys.

Example:

socket.rooms[keys[0]] = WMWlX-ekAxa8hP8FAAAE

socket.rooms[keys[1]] = app-chat-room

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda