Gihan Lakshan
Gihan Lakshan

Reputation: 403

Is storing socket objects in Map can cause issues in a production environment with larger amount of clients?

I'm using ocpp-rpc package to run an OCPP server, currently I'm using Map to store the connected clients object, I want to reference them later.

I'm storing them like below

const connectedChargers = new Map();

server.on('client', async (client) => {
    console.log(`${client.identity} connected!`);

    connectedChargers.set(client.identity, client); // storing client reference
});

I want to know is this a best practice to do? Is there any better way to achieve this? My system going to have thousands of clients that connecting to the server, so I need optimal way to handle this.

Also I tried to save them in the Redis, but it fails due to circular references.

Basically I need to know that my aproach is good or bad and if it is bad a better way to achieve this.

I tried to save them in the Redis, but it fails due to circular references. I want

Upvotes: 0

Views: 55

Answers (1)

Amirreza Karimyrad
Amirreza Karimyrad

Reputation: 141

What you are doing is fine, just make sure to clean them up upon disconnection. Something like this:

const connectedChargers = new Map();

server.on('client', (client) => {
    connectedChargers.set(client, client.identity);
});

server.on('disconnect' (client) => {
    connectedChargers.delete(client.identity);
});

Keeping a reference of the client object in a map is not concerning, even if you keep a million of them. The client object is kept in the ram anyway, you are just saving a key and a reference to the object.

Your real concern should be bandwidth, the amount of ram os need to keep a socket open and most importantly, the CPU usage required to process the messages received by those thousands of clients, specially if they are going to send a lot of messages!

Upvotes: 1

Related Questions