It became unclear:

each time the user updates the page, a new connection object is created on the server.

Question: How to keep this object relevant?

It occurred to keep this connection in the БД and when creating a new connection, update the value from this БД for a specific user ... that is, there is some structure:

 user_id | socket 34 | {obj} 

there is a user with ID 34 , he has his own ws connection object, which is stored in the socket , and as I said, - each time a new connection is created, the values ​​from the socket field will be updated

Do you say it right?

PS For a question and answer site

  • well, just use the base for this is not necessary - just an object in memory. - Vadim
  • 3
    minus without telling the reason is a stupid action. for use websocket this question is very relevant. - Vadim
  • Thanks for the answer! But how can I distinguish these objects? What object to which user to treat? use a pair of "key: value"? - cmd
  • yes, like this: login: connection. or as an option, if there is a possibility, register the name in the connection itself, as an additional value. - Vadim
  • only security has to be taken into account - enter the key in this "array": a value only when there is a key, i.e. user logged in. otherwise break off the connection - Vadim

2 answers 2

It occurred to keep this connection in the database

Ho ho o! Um, no, this is a bad idea. A connection is very contextual for a specific process and should not be put into a public repository, since if another process takes it from there, it will not receive a working connection, since the OS allocated resources for this to another process.

... this is conditional if you have a connection object serialized into a sequence of bytes for writing to the database (the database stores the data ). But most likely, the whole undertaking will end at this stage already, since you simply cannot get the data necessary for reproduction even within the framework of the same process .

Plus you need to keep in mind that a user can be on the site from several tabs and / or even devices at the same time , and probably you need to be able to keep several active connections per user at a time. Do you already feel this spirit of adventure?

If you work with just one process, then you can at least store them in a global object. socket.io does something similar to socket.io by default, and socket.io-express-session from the @Darth response will help you quickly map users and connections.

But this is not the end of the story if your application runs in several processes . Because the user, having connected to the process, hangs only on it. It will be impossible to send a message to it from another process of your application. For this, socket.io-adapter (abstract) and its implementations are socket.io-redis , for example socket.io-redis , which provides communication between application processes through Redis. In this case, each process still keeps its connections somewhere in itself, in a global object, but other processes can still access them anyway.

    Use the socket.io-express-session module. Then you will be able to enjoy all the benefits of the sessions and you will not need to invent a bicycle in order to transfer the user id every time you reconnect