Henry Boldizsar
Henry Boldizsar

Reputation: 489

How to subscribe to an instance room where an attribute besides id is true in Socket.io/ SailsJS?

How do you make sockets subscribe to an instance room where an an attribute besides ID is true? My situation is I have a notifications model I want users to subscribe to but only when the owner attribute equals the user's id.

Here's what I've tried:

Notification.subscribe(req.socket , { owner: req.session.User.id } );

Upvotes: 3

Views: 79

Answers (1)

alexv
alexv

Reputation: 36

In the case of Notifications, the client should subscribe to the instance room of its own logged in user id. That looks like this:

Notification.subscribe( req.socket, req.session.User.id );

Then, when a Notification is created, you can use the 'publishUpdate' method to publish the notification to that specific room:

Notification.publishUpdate(ownerId, notification);

Where ownerId is equal to the user's id who should be receiving the notification.

So for example, if a "like" on a post creates a Notification in the like controller, then the Notification is published (using publishUpdate with the post owner's id) then the post owner will receive the notification at the client. The client can subscribe using:

socket.on('notification', cometMessageReceivedFromServer);

assuming that the model name is "Notification".

Upvotes: 2

Related Questions