#Running Multiple Clients and Storing them into global variables?

12 messages · Page 1 of 1 (latest)

civic heath
#

If I want to run multiple Client instances + send messages with them using express.js api, do I need to loop and instantiate new Client({ authStrategy: new LocalAuth({ clientId: "something" }) }) and store each of them into a global var so I can call Client.sendMessage() later inside my express.js anywhere?

spring sail
#

I'm using an object that stores in each key an instance

civic heath
#

@spring sail hi can you show a simple example of how you do it please?

spring sail
#

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;

civic heath
spring sail
#

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.

civic heath
#

@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?

spring sail
#

I use that both in the warmup and every time it creates a new instance

civic heath
#

@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.
civic heath
spring sail
#

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

pale light