N
N
Nikita Reshetnyak2019-11-21 14:14:24
Node.js
Nikita Reshetnyak, 2019-11-21 14:14:24

How to pin socket.io sessions to a user?

Hello, there is a need to bind socket.io sessions to a user. I did the binding from the beginning by creating a user_id:socket_id pair in redis, but when opening a new tab, the socket_id was overwritten and it turned out that the previous session was "outdated".

Example :
The site has a list of notifications, while one tab is open, the socket updates this list according to a certain event, but if the user opens another tab, the value for user_id will be overwritten. From here it turns out that the list of notifications will be updated only on a new page.

Question :
How to properly organize the assignment of socket_id to the user so that there are no incidents like a new open tab?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Virsky, 2019-11-21
@MeG1tsune

It is possible to send an event on the client immediately after connecting, in which to transfer the user's token. And the server, when receiving this event, stores the correspondence between the key and socket_id in an array (or in a list if it is a radish). And then when the server needs to send a message to the user, it sends the message to all the socket_ids associated with the passed user token.

I
Ivan Shumov, 2019-11-21
@inoise

we take a normal database and make something like this structure there:
sessions:
user_id | session_id is deleted
when disconnected, added when connected. To send to all sessions - select by user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question