Save websocket id

What would be the correct way to store a user’s websocket id? Of course, every time the user disconnects and connects it generates a different id, could it be saved in a database? would that be performatic?

Hello @diegofullstackjs

It depends on your performance criteria. If connection between DB and Adonis is fast (they are in same server for example), you can spare some ms and need data to be there after API restarts (new deploy for example) then DB would be good solution for you. To get more speed could add some cache for it like Redis or Memcache.

But if hitting DB / cache is too slow then you might want to keep IDs in memory, in some object for example, and access IDs directly from there. It will be the fastest option, but when API restarts, then you lose track of all IDs

so I believe that it would be best to store the socket id in a database and every time there is an event with a new connection it will be updated in the database and when the client disconnects it will be deleted from the database

1 Like

Another thing is amount of connections to keep in mind. When you get several hundred connections per sec, then it will start to hurt your DB (depending on server and DB tho)

But when it’s regular website / admin panel for company then it shouldn’t be a problem

If DB is an option for you, I’d start with it and when it starts to bottleneck in future then move to some cache

1 Like