Thursday 20 April 2017

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);
            })
    }



via sanket

No comments:

Post a Comment