users[name].emit('private_msg', {msg : msg, user:socket.username});
TypeError: users[name].emit is not a function
I am unable to solve this error, how can i fix this??
I am developing a chat application using nodejs and socket.io and here users{}; is an object which contains connected users, and I want to use them as socketid, to add functionality of private messaging.
full code :-
//Send Message
socket.on('send message',function(data,callback){
var msg = data.trim();
if(msg.substr(0,3) === "/p ")
{
msg = msg.substr(3);
var ind = msg.indexOf(' ');
if(ind !== -1)
{
var name = msg.substring(0,ind);
var msg = msg.substring(ind + 1);
if(name in users)
{
console.log(users[name]);
users[name].emit('private_msg', {msg : msg, user:socket.username});
console.log('Prviate' + users[name]);
}
else
{
callback('Error!, user not found');
}
}
else
{
callback('plz enter msg');
}
}
else
{
io.sockets.emit('new message',{msg : msg,user:socket.username});
}
});