Suresh
Suresh

Reputation: 5987

How to store user's session info in node js chat application?

I've build a basic chat module by using node js & socket io. I know javascript but pretty new in node & socket area. Now i'm going to integrate this chat module in one of our application, where employee of different organization will be able to communicate with each other. Now, the problem which i'm facing is - can't able to know where to store connected user's session info.

What i want to do is , Suppose there are 2 organization A & B. When any employee of organization 'A' will connect with node socket server i'll create a session variable(May an ARRAY) for Organization-A & store that employee inside that array. Same for organization B also. In this way i want to create one kind of bucket structure for each organization & respective employee will be in those buckets.

So, when any new employee of an organization get connect with socket server i'll check to which organization that employee belong to & inform only those organization's employee only. The problem i'm facing at here is - Where to store all these connected user's information. Is Node Socket serve is the right place to store those data or i should be use some other mechanism like REDIS(http://redis.io/) ?

Any idea!!!, what's the standard way to handle this kind situation ?

Upvotes: 0

Views: 931

Answers (2)

Tomas Brambora
Tomas Brambora

Reputation: 1046

Yes, preferably store them in Redis.

If you'll want to scale (e.g. increase the number of Heroku dynos), keeping the data in memory is an anti-pattern (read: will prevent you from scaling).

Upvotes: 1

Alex J
Alex J

Reputation: 1019

I'm guessing they'll need some sort of credentials for that. Using the docs http://socket.io/docs/ as a reference, you could do something like:

io.on('connection', function (socket) {
  //some code to filter your users between organization A or B 
}

and then you could send info to specific users through

// iterate through each user in organizationA:
socket.emit('orgnizationA', someData);

Upvotes: 0

Related Questions