I have node up and running with socket.io. I am able to send messages to people who are online on my server(who have their socket connection open). But when i try to send it multiple people with atleast one offline it doesn't go through. if all are online it goes through.
Any help on how to fix this?
client.on('message_from_client',function(data){
for(var i=0;i<data.length;i++){
if(data[i].message_to!=''){
client.emit("update_message_from_server", data[i]);
if(data[i].message_to != client.username){
var message_to = data[i].message_to;
var array =data[i];
redisClient.SISMEMBER('online',message_to,function(err,reply){
if(reply!=0){
redisClient.get(message_to,function(err,reply2){
if(reply2!=null){
io.sockets.sockets[reply2].emit("update_message_from_server",array );
}
});
}else{
console.log("Offline");
}
});
}
}else{
client.emit("update_message_from_server", data[i]);
}
}
});
data is a JSON object. If i have any data[i].message_to as offline i am only able to send this to client but no other sockets. If i have all entries online i am able to send it to both client and other sockets
I figured out other way to do this
client.on('message_from_client',function(data){
alldata = data.data;
console.log(alldata);
alldata = JSON.parse(alldata);
alldata.forEach(function(data){
if(data.message_to!=''){
client.emit("update_message_from_server", data);
if(data.message_to != client.username){
redisClient.SISMEMBER('online',data.message_to,function(err,reply){
if(reply!=0){
redisClient.get(data.message_to,function(err,reply2){
if(reply2!=null){
io.sockets.sockets[reply2].emit("update_message_from_server",data );
}
});
}else{
console.log("Offline");
}
});
}
}else{
client.emit("update_message_from_server", data);
}
});
});