I'm using Socket.Io 1.7.3 on Angular 2 , which connect to ExpressJS Server. I can't send the package to the specific socket id, event they are actually match.
Server implementation:
socket.on('subscribeNotification', function(data){
// Get new notification by user_id here ....
console.log('Got notification request');
console.log('Your Id is: '+socket.id);
socket.broadcast.to(socket.id).emit('sendNotificationsToUser', {
text: 'hello',
id: socket.id
});
socket.emit('claimId', {
id: socket.id
});
});
And on Angular 2 Implementation:
subscribeNotificationEvent(userId: string) {
let socket = this.socket;
let data = {
user_id: userId
};
// Emit socket
socket.emit('subscribeNotification', data);
}
listenToNotification() {
let socket = this.socket;
socket.on('sendNotificationsToUser', function (data) {
console.log('I hear it !');
});
socket.on('claimId', (data) => {
console.log('My id is: ' + data.id);
})
}
Logging Result
Server:
Got notification request
Your Id is: bSCHMUL2bJJQCXkxAAAC
Client:
My id is: bSCHMUL2bJJQCXkxAAAC
The client doesn't receive sendNotificationsToUser
event, so it cannot log out the line 'I hear it !'
.
I have also refered to Sending message to specific client in Socket IO , but it doesn't work.