Wating for Http request makes node-red unresponsive / freezes

Hi,
I'm trying to do a node like "http request", i used the source of this node as inspiration,
but when i do a request that the remote address takes some time to respond or don't respond at all, my node-red gets stuck.
The "http request" node doesn't do that.

I tried with the library "Got" with the same result.

node.on('input', (msg, send, done) => {
    request('http://192.168.200.220:1880/test', function(err, res, body) {
        if(err) {
            msg.payload = 'Error';
            msg.statusCode = 404;
            node.send(msg);
            done();
        } else {
            msg.payload = body;
            msg.statusCode = 200;
            node.send(msg);
            done();
        }
    });

  /*(async () => {
        try {
            const response = await got('http://192.168.200.220:1880/test');

            if(response !== undefined) {
                msg.payload = response.body;
                msg.statusCode = 200;
                node.send(msg);
                done();
            } else {
                msg.payload = "Error";
                msg.statusCode = 404;
                node.send(msg);
                done();
            }
                    
       } catch(error) {
           msg.payload = 'Error';
           msg.statusCode = 404;
           node.send(msg);
           done();
       }
   })();*/
});

I use a disconnected "http in" node to test a non responsive http request.

image

Thanks.

I've been using the Axios library to help with my smart heating module node-drayton-wiser and that seems to work OK with Node-RED.

I've included require in the globals in settings.js on my dev machine and then have some functions that look like this:

/** Create class instance and try to connect */
const require = global.get('require')

const wiser = require('node-drayton-wiser')()

wiser.setConfig({
    ip: msg.payload.host,
    secret: msg.payload.secret,
})

//wiser.debug()

// Quick Connection Test
wiser.testConnection()
    .then( d => {
        msg.payload = d
        msg.topic = 'Wiser Test Connection'
        node.send(msg)
    })

// Test get of specific controller data section
wiser.get('network')
    .then( res => {
        if ( res.error ) {
            msg.payload = res.error
            msg.topic = 'Wiser Get ERROR'
            node.send(msg)
        } else {
            msg.payload = res
            msg.topic = 'Wiser Get SUCCESS'
            node.send(msg)
        }
    })
    .catch( err => {
        msg.payload = err
        msg.topic = 'Wiser Get ERROR (catch)'
        node.send(msg)
    })

//return msg

The functions in my module are mainly Promise-based since that's what Axios uses.

Do you really want to do the require every time the function is called ?

:smiley: It is only a convenience for testing Dave. I wouldn't do that on a live system. Ultimately, it will all get written into a custom node of course, this is just a quick and easy way to get started.

It would, of course, be nice if the setup tab allowed you do set a variable that the main tab could consume. Sorry, didn't explain that well. I mean that a const x = "something which would mean that you could also create a function at startup that the function node could then use.

iindeed you can write to context from the setup and then retrieve in the main as now - but yes would be nice if more direct.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.