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

219
Visualizações
socket.emit not emitting messages to the sender
  1. 'socket.emit' not sending messages to the client which sent it,while
  2. 'socket.broadcast.emit' sending messages to all the clients including the sender.

I cannot figure out what I am missing here. For the second case, I checked for socket.id while sending the message and logged it when response is received,it turned out to be the same

socket.js

var socket = require('socket.io')(),
socketApi = {};

socketApi.io = socket;

socketApi.io.on('connection',(client) => {
    client.on('clientMessage', (msg) => {
        console.log('hi');
        client.emit('serverMessage',msg);
    })
    client.on('disconnect',() => {
        socketApi.io.emit('serverMessage','client is disconnected');
        console.log('disconnected');
    })
})


module.exports = socketApi;

client.js

getServerResponse() {
        socket.on('serverMessage',(msg) => {
                console.log(msg);
            })
    }
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

According to the docs broadcast.emit sends to all except the sender and emit sends to the sender only: https://socket.io/docs/emit-cheatsheet/.

I was in the same issue so I ended up calling both:

// socket.io server
io.on('connection', socket => {
  socket.on('message', (data) => {
    messages.push(data)
    socket.broadcast.emit('message', data) // sends to all except the sender
    socket.emit('message', data)  // sends to the sender
  })
})
about 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