Cris
Cris

Reputation: 12214

Storing data in arrays in node.js

I'm experimenting node.js building a chat server app so i'm trying to maintain client connections in arrays, incrementing index for every new connection inserting the client object in an array. I don't like this solution because i think it's too much light, i put holes in arrays for every disconnection and find long to iterate in arrays for finding a certain connection. Which is the "node" way to handle multiple connections?

Upvotes: 0

Views: 102

Answers (1)

Nick Mitchinson
Nick Mitchinson

Reputation: 5480

For the record, my understanding here is that you're trying to track clients to know who to broadcast to? I think this based on a previous question you asked that I saw.

I believe that this is similar to how socket.io handles rooms internally. It's been a while since I actually looked into it, but I believe this is how it's done. That being said, what I've done in the past is use rooms for each chat. Something along the lines to a concatenation of the usernames/userid's of the members. This way when you get data on a connection, you can easily broadcast it back to the same room, which will send it to all connections in the room except for the one it received it from. Perfect for a chat application. Socket.io will handle tracking which connections are in which room.

Upvotes: 1

Related Questions