node.js - Send update notification to particular users using socket.io -
The code at the following conclusion is, where FYI - node version 1.1.0 The path file contains the server code - console 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 - But I am in a dilemma that what should I do to store more Edit - Add Controller - (Front End) Front End Interface where memorandum is created and sends to special users Front end interface where notification "notification code" to display The server at the end of the code - < / Strong> Backend controller code - 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 The server saves sockets for each user ID. If you have such a mapping in the server then you can send a message to this client using the User ID. Edit: It is even better to save this socket directly. storeSelUserId contains user_id to send a message -
// socket notification var socket = io ('http: // localhost: 6868'); Socket.on ('Connection', Function (Data) {socket.emit ('Send Notice', {sent_to: storeSelUserId});});
var client = {}; Io.on ('connection', function (socket) {socket.emit ('connection', 'connection created.)); Socket.one ('notification', function (sent) {console.log (sent_to);});}); The array of
sent_to appears in
user_id .
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);});});
user_id which refers to socket_id and then the devices of users with that particular ID Update?
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)
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;})}}
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);});});});
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 ++; }};
socket.on ('connection', function (data) {socket.emit ('setUserId', myUserId);});
socket.on ('setUserId', function (userId) {user [userId] = socket;});
socket.on ('send notification', function (user id) {user [user id]. MIT ('notification', 'important information message');});
Comments
Post a Comment