javascript - How to listen for a socket update on the logged in user in all pages in Sails.js? -


i have sails.js app.

there selection current settings in each page (set in layout).

i need able listen on pages current changed event , notify user refresh page. can call io.socket.on('currentchanged') in client side , run emit in currentcontroller.changed? or need listen current controller , wrap response in if(req.issocket)?

and how make sure notify user, not everyone?

is there better way? don't want add same socket response every single listener.

i have looked on stack overflow , google , can't come listener pages @ once.

edit: aware can session id using req.socket.handshake.sessionid. can raw socket rather 'sails.js' way.

also, in link suggests can join room using user id. should on login

well, answered question this, situation can modified this:

you better create different rooms users.

create controller:

notificationscontroller.js

module.exports = {      subscribe: function(req, res) {         // userid of user method         .....         .....         var roomname = 'user_' + userid;         sails.sockets.join(req.socket, roomname);         res.json({             room: roomname         });     } } 

somewhere can create notification:

var roomnameforuser = 'user_' + userid; sails.sockets.blast(roomnameforuser, 'currentchanged'); 

and in view:

io.socket.on('connect', function(){     io.socket.get('/notifications/subscribe', function(data, jwr){         if (jwr.statuscode == 200){             io.socket.on(data.room,function(obj){                 console.log(obj);             });         } else {             console.log(jwr);         }     }); }); 

also must add policy config/policies.js like:

  'notificationscontroller': {     '*': ['passport', 'sessionauth']   }, 

of cource, if use passport or sails-auth.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -