node.js – Node-Red: external system connection sharing

I am developing a new inject node with a new configuration node for connecting to a 3rd party system.

I’d like the configuration node to maintain an actual connection to the system and share it with UI nodes, instead of just storing the configuration.

The problem I am facing is that the procedure of creating the connection (in config node) is asynchronous and it finishes AFTER the UI nodes finish initializing. To properly initialize the UI nodes, I need the config node initialization to finish first.

This is my config node pseudo code:

module.exports = function(RED) {


    function FakeConnectionNode(config) {
        RED.nodes.createNode(this,config);

        (async function initializeNode(self) {
            const reconnect = () => new Promise(resolve => {
                reconnectTimeout = setTimeout(async () => {
                    try {
                        await initializeNode(self);
                        resolve();
                    }
                    catch (e) {
                        await reconnect();
                    }
                }, 2000);
            });
            try {

                    await init_connection('new_connection', {
                          some config params
                    }, async () => {
                        console.debug('connection created');
                    }); 
             }
            catch ...
        })(this);
    }
    RED.nodes.registerType("fake-connection",FakeConnectionNode);
}

Read more here: Source link