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++) {