javascript - Sails js subscribe to model changes scoped by groupid attribute -
i have model named groupfeed looks this
module.exports = { schema:true, attributes: { groupid: { model:'groups', required:true }, postid: { model:'post', required:true }, objectid: { model:'objects', required:true }, } };
on client side can subscribe groupfeed model using
io.socket.get('/groupfeed')
which done automatically blueprint api ,
io.socket.on('groupfeed',function(obj){console.log(obj)})
would give me updates on model changes when publish on backend using
groupfeed.publishcreate({id:4,groupid:6,postid:2,objectid:1})
what want :-
i want client subscribe groupfeeds particular groupid. eg: user x can subscribe groupfeeds groupid 1 (note: group model stores user membership group )
or imaginary call:
io.socket.get('/groupfeed?groupid=5')
so when call publishcreate groupid:5, people subscribed groupid 5's groupfeed update
you better create different rooms groups.
code untested! create controller: notificationscontroller.js
module.exports = { subscribe: function(req, res) { // groupid of user method ..... ..... var roomname = 'group_' + groupid; sails.sockets.join(req.socket, roomname); res.json({ room: roomname }); } }
somewhere can create notification:
var roomnameforgroup = 'group_' + groupid; sails.sockets.blast(roomnameforgroup, {id:4,groupid:6,postid:2,objectid:1});
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); } }); });
i can not test code right now, looks workable.
Comments
Post a Comment