Send update notifications to specific users using socket.io

Below is the code at the front end, where storeSelUserId contains user_id for sending the message -

FYI - Node Version 1.1.0

 // Socket Notification var socket = io('http://localhost:6868'); socket.on('connection', function (data) { socket.emit('send notification', { sent_to: storeSelUserId }); }); 

Below is the server code in the routes file -

 var clients = {}; io.on('connection', function (socket) { socket.emit('connection', "Connection Created."); socket.on('send notification', function (sent_to) { console.log(sent_to); }); }); 

The sent_to console displays the user_id array.

Now, as a starter in socket.io , I'm stuck with the decision on how to send a message to these specific users.

I searched and found that I need to push each user with my sockets so that I change it -

 var users = []; io.on('connection', function (socket) { users.push({socket_id: socket.id}); socket.emit('connection', "Connection Created."); socket.on('send notification', function (sent_to) { console.log(sent_to); }); }); 

But I'm in a dilemma, what else do I need to do to save that user_id refers to which socket_id, and then update the user div with these specific identifiers?

EDIT -

Add Controller - (Front End)

The user interface interface where memo is created and sent to specific users

 var socket = io('http://localhost:6868'); socket.on('connection', function (data) { socket.emit('send memo notification', {creator_id: creator_id, sent_to: [Array of user_ids to whom memo to send]}); }); 

Control Panel Controller - (Front End)

The front-end interface where the notification count shows "notificationCount"

 if (SessionService.currentUser._id) { var socket = io('http://localhost:6868'); socket.on('connection', function (data) { socket.emit('get notifications', {user_id: SessionService.currentUser._id}); }); socket.on('notification data', function(data){ console.log("-- Not Data Test -"); $scope.notificationCount = data.length; }); } 

The code on the server is

 io.on('connection', function (socket) { socket.emit('connection', "Connection Created."); socket.on('send memo notification', function(data) { notifications.createNotification(data); }); socket.on('get notifications', function(data){ notifications.getNotifications(data, function(response){ socket.emit('notification data', response.data); }); }); }); 

The backend controller code is

 exports.getNotifications = function(data, callback) { var userId = data.user_id; Notification.find({receiver_id: userId}, function(err, response){ if (err) callback({"message": "error", "data": err, "status_code": "500"}); else callback({"message": "success", "data": response, "status_code": "200"}); }); }; exports.createNotification = function(data) { var notificationData = data; var x = 0; for(var i=0; i< notificationData.length; i++) { // Code Notification(notificationData[i]).save(function(err,response){ if (err) return false; }); if (x === notificationData.length - 1) { return true; } x++; } }; 
+5
source share
2 answers

If you want to use your own user IDs, then there is no way to match the socket ID with the user ID. I assume that the client knows his user ID from somewhere, so he could send his user ID to the server after connecting.

Client

 socket.on('connection', function (data) { socket.emit('setUserId', myUserId); }); 

The server stores a socket for each user ID.

 socket.on('setUserId', function (userId) { users[userId]=socket; }); 

If you have such a mapping on the server, you can send a message to this particular client using the user ID.

 socket.on('send notification', function (userId) { users[userId].emit('notification', "important notification message"); }); 

Edit: Saving the appropriate socket directly is even better.

+18
source

In accordance with what I understand, you need to send a private notification only to some users. To do this, save the name of the user you want to send and the corresponding socket in different hashes.

 username [socket.name] = username to be added; usersocket [ socket.name ] =socket; 

Then, to emit messages only to this user, use

 usersocket[ socket.name ].emit('event for send message', ' what you want to send '); 
+3
source

Source: https://habr.com/ru/post/1204407/


All Articles