Search code examples
javascriptexpresssocket.io

Can using socket.request.session.reload() be avoided when using express-session


I am making a web app that uses express, socket.io. I use express-session throughout the code, as a way of storing attributes for the client. However, I need to access the latest version of this in socket.io. Is there a way to achieve this without having to use socket.request.session.reload(()=>{})?

I tried using Redisstore as a store for the express session, however it did not seem to have an effect. I only used this as the store: new Redisstore({client: redisclient}). Is there something I could have done?

Alternatively, does anyone know of a way to prevent race conditions, where socket.io is saving, or using the data, whilst the express reloads (or vice-versa), causing sync issues?


Solution

  • Though not the greatest soloution possible, I found a decent method: Use redisjson as the store, which allows for (though async and still a risk through getting them) only changing certain parts of the data. It also cut the time to save the session down (8-12 --> 6-8 ms), though this may not be entirely accurate timeings, as only small amounts of testing was done. Furthermore, it allowed for saving of only one piece of the data efficiently (reducing the chance for a collision, and performance overheads) in as little as 2ms