#Running Multiple Clients and Storing them into global variables?
12 messages · Page 1 of 1 (latest)
I'm using an object that stores in each key an instance
@spring sail hi can you show a simple example of how you do it please?
My method, with no guarantee of being the best
I declare a global object
export let instances = {};
createSession is a function that initializes and returns Client()
Put the result in the object, with any unique deviceId being the key.
let newSession = await this.wwjsService.createSession(deviceId);
instances[deviceId] = { client: newSession };
As an alternative to putting it in an array, so I don't have to loop it to find the instance that I'm searching for.
I access it like this:
let foundInstance = instances[deviceId].client;
I see, for createSession do you mean you actually stored these clients object directly into server session? I read if objects are stored to session their methods will no longer work, because the objects get serialized?
In createSession I create, associate with events, initialize and then return Client(). It's just a wrapper function of what you find in the official guide.
It works, methods too. Try it.
@spring sail this:
let newSession = await this.wwjsService.createSession(deviceId);
instances[deviceId] = { client: newSession };
is inside a loop inside the main .js file server.js/index.js right? not some class?
I use that both in the warmup and every time it creates a new instance
@spring sail for warmup do you mean like to refresh the whatsapp web instance so it does not logout in 30 minutes? or to refresh whatsapp web instance every 14 days?
30 minutes:
How long does WhatsApp Web stay connected? You will be automatically logged out of WhatsApp Web after 30 minutes of inactivity. When you sign into WhatsApp Web, you can check a box under the QR code which says keep me signed in. Then you will stay connected as long as WhatsApp is connected on your phone.
14 days:
How long does WhatsApp web Stay Connected 2022?
Your phone doesn't need to stay online to use WhatsApp on linked devices, but your linked devices will be logged out if you don't use your phone for over 14 days.
are each of the newSession objects a singleton itself?
warmup is when the app is restarted for any reason and you have to reload in memory all the instances that you previously have associated
Hi! Sory for asking, could you share the code?