'ExpressJS inject async dependency (WWEBJS) once and start listening to http requests

Hello I have an express node backend with WWEBJS (multidevice auth) that upon initialization (via localauth) will start an instance that is readily available to start sending messages, after the following event.

client.ready(function() { });

However WWEB localauth made some breaking changes as session auths are no longer possible, so best it can do is to initiate a single instance via local auth.

I have no idea what I am doing, so I am trying to implement IOC with no avail.

clientjs

 module.exports = function (c) {
        c.service('client', async function (c) {
            const {
                Client,
                LocalAuth
            } = require('whatsapp-web.js');
            let client = new Client({
                authStrategy: new LocalAuth({
                    dataPath: './.wwebjs_auth/',
                    clientId: 'client-one'
                }),
                puppeteer: {
                    headless: false
                }
            }); 
            await client.initialize();  
            await client.on('ready', () => {
        
                var data = {};
                data.number = "#censored"
                data.msg = "Ready up ready up";
    
                client.sendMessage(data.number, data.msg);
        
            });
            client.on('authenticated', (session) => {
                console.log('AUTHENTICATED', session);
            });
            client.on('auth_failure', msg => {
                console.error('AUTHENTICATION FAILURE', msg);
            });
        });
    }

createContainerJS

 class Container {
        constructor(){
            this.services = {};
        }
        service(name, cb){
            Object.defineProperty(this, name, {
                get: () => {
                    if(!this.services.hasOwnProperty(name)){
                        this.services[name] = cb(this);
                    }
                    return this.services[name];
                },
                configurable: true,
                enumerable: true
            });
            return this;
        }
    }

module.exports = function(){
    let container = new Container();

    require('./client.js')(container); //singleton
    // require('./express')(container)
    return container;
};

And on my express server

  let createContainer = require('./createContainer');
    let c = createContainer();
    var app = express();
app.get('/test-whats', async(req, res, next) => { //minimal test to verify wweb is working at all

        let resp = await c.client
        res.json([resp]);

});

Is Inversion of Control the best approach here? Is there a better way to inject client js as a dependency so my express routes can access the client ? I'll definitely appreciate someone to put me in the right direction



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source