Sending special session messages using socket.io and sails.js

I am trying to implement private chat functions using the sails.js framework and I am having problems trying to send a message to a specific user.

Currently, I have achieved private communication by sending messages to a specific socket.id using socket.io .socket(socket.id).emit(event,message) , but the problem with this approach is that every time the user opens a new tab, new socket.id, for this new connection.

And my question is: does sails.js do a way to emit events (using socket.io) for a specific user session instead of a bunch of socket identifiers? is this possible with these technologies?

Therefore, I can send an event only once and make sure that it is received on all the tabs in which the chat application is now open.

Thanks in advance.

+2
source share
1 answer

Sails complement all their models with some useful pubsub methods. Whenever you find a model through a socket request (for example, socket.get('/user/123') ), the requesting socket automatically subscribes to all messages about this model. Then you can call User.message(123, data) to send a message to all connected sockets for this user.

Docs for pubsub methods here .

+3
source

Source: https://habr.com/ru/post/1215195/


All Articles