I have in my Express
server
userbase = {'Fitz': '84Iw3MNEIMaed0GoAAAD'}; //Exapmle user to receive message and associated socket id
//Sending message to Example user
socket.on('send_msg',function(data_from_client){
//Testing for receivers socketId
console.log(userbase[data_from_client.to_user]);//This returns the socket id for Fitz in userbase successfully i.e 84Iw3MNEIMaed0GoAAAD
socket.broadcast.to(userbase[data_from_client.to_user]).emit('get_msg',{msg:data_server.msg});
});
Surprise surprise when I setup a handler for this event on my cliens side for 'get_msg'
i get nothing.
.factory('SocketFctry',function(){
var socket = io('http://localhost:3002')
return socket;
})
.controller('ChatCtrl', function($scope,SocketFctry) {
SocketFctry.on('get_msg',function(received_message){
console.log(received_message);
$scope.$apply();
})
});
My other client side handlers are working fine.
SocketFctry.on('new_user', function(users,my_id){
console.log(users);
$scope.online_users = users;
$scope.$apply();
})
My version of socket.io is 1.3.7 .Am I missing something here?
Do you have users join a namespace on connection?
You can try:
socket.broadcast().to()
send a message to all users that match theto()
arguments EXCEPT the user who'ssocket
it is. So,socket.broadcast.to(socket.id)
will never send to the actualsocket
user. In fact, by default, it won't send to anyone.Direct from the socket.io doc:
If you want to send to only a single socket, then just use:
If you want to broadcast to a socket.id-type room and you want to include the user who's room it is, then use:
So, you can change this:
to this: