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.