node.js - Send update notification to particular users using socket.io -


The code at the following conclusion is, where storeSelUserId contains user_id to send a message -

FYI - node version 1.1.0

  // socket notification var socket = io ('http: // localhost: 6868'); Socket.on ('Connection', Function (Data) {socket.emit ('Send Notice', {sent_to: storeSelUserId});});   

The path file contains the server code -

  var client = {}; Io.on ('connection', function (socket) {socket.emit ('connection', 'connection created.)); Socket.one ('notification', function (sent) {console.log (sent_to);});}); The array of   

console sent_to appears in user_id .

Now there is a starter in Soft.io I got stuck with the solution how do I send a message to these specific users.

I searched and found that I need to push each user with its sockets, so I improved it -

  var user = []; Io.on ('connection', function {users.push ({socket_id: socket.id}); socket.emit ('connection', 'connection created.'); Socket.on ('Send notifications' Function (sent_to) {console.log (sent_to);});});   

But I am in a dilemma that what should I do to store more user_id which refers to socket_id and then the devices of users with that particular ID Update?

Edit -

Add Controller - (Front End)

Front End Interface where memorandum is created and sends to special 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 URI to send to Memo}});};  < / P>  Dashboard Controller - (Front End)   

Front end interface where notification "notification code" to display

  if (session service service user ._id) {var socket = io ('http: // localhost: 6868'); Socket.on ('connection', function (data) {socket.emit ('notification received) Do ', {user_id: SessionService.currentUser._id});}); Socket.on (' Information Data ', Function (Data) {console.log ("- not a data test -"); $ scope.notificationCount = data.length;})}}   

The server at the end of the code - < / Strong>

  io.on ('connection', function (socket) {socket.emit ('connection', 'connection created.'); Socket.one ('Send Memo Notification ', Function (data) {notifications.createNotification (data);}); socket.on (' get notifications', function (data) {Notifications.getNotifications (data, function) {socket.emit ('information data' , Response.data);});});});   

Backend controller code -

  export.getNotifications = function (data, callback) {var userId = data.user_id; Notification.Find ({receiver_id: userId}, function (error, response) {if (err) callback ({"message": "error", "data": err, "status_code": "500"}) else callback ({"Message": "Success", "Data": Response, "Status Code": "200"};}); }; Export.createNotification = function (data) {var information data = data; Var x = 0; (Var i = 0; i & lt; Notification.Data.label; i ++) {// Code Notification (Notification Data [i]) Save (Function (Error, Response) {If (mistake) Return False;}) ; If (x === Notification Data.length - 1) {back true; } X ++; }};    

If you want to use your own user ID then someone around the mapping There is no way socket id on user ID, I believe that a customer knows his user ID from anywhere, so he can send his user id to the server after connection.

Customer

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

The server saves sockets for each user ID.

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

If you have such a mapping in the server then you can send a message to this client using the User ID.

  socket.on ('send notification', function (user id) {user [user id]. MIT ('notification', 'important information message');});   

Edit: It is even better to save this socket directly.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -