Serializing, storing, and deserializing WS objects

I am using NodeJS ws library to build an application. I am currently using an object to store instances of user Web site connections with a unique user ID as the key. Pseudo-code:

let userWebSockets = { 'user-guid-here': WSConnection
};


This works great, and when I need to target a specific user in order to send a message back, I can grab their connection from the entity userWebSockets

this way let ws = userWebSockets['user-guid-here']

.

The problem is that if I have a Node cluster processing in a box, I start, deploy a master process using a cluster module or cluster fields, then there is no guarantee that the same object userWebSockets

will be in memory and when I try to find a custom connection based on their unique identifier, I risk getting it undefined

as a return value from the search.

My first wish was to use some kind of k => v remote store like Redis to handle the supporting dictionary, however I realized in my implementation attempt that Redis is obviously not going to store a Javascript object and the best I could What to do with such technology is to serialize the object in such a way that it can later be restored / deserialized. I tried to use Crockford cycle.js from the following repository: https://github.com/douglascrockford/JSON-js/blob/master/cycle.js It failed as I found the method prototypes of the object I was trying to get back to life, have not survived ( ws.send()

in this case, for example).

My current workaround is to use nginx to balance in an upstream pool of instances and use the ip_hash directive to make the user ip => an instance sticky, however I am not satisfied with this solution.

Is anyone really lucky with what I'm trying to do here? I am also open to re-archiving the application if there is a better or more idiomatic way to handle user associations => ws.

+3


source to share





All Articles