Node to check if there is Internet connection or a specific server is online

3,222 views
Skip to first unread message

Alexander Pivovarov

unread,
Mar 12, 2016, 3:44:50 PM3/12/16
to Node-RED
Hi, 

If you use http request and the Internet connection or target server is down node red generate errors in debug log and console. The same is if you are trying to send email if there is no connection. As I prefer to be able to handle such situations I wrote a node Is Online. It checks if there is the Internet connection or the particular server is up. There are three modes: 
(1) It can send true or false as msg.payload
(2) It can stop sending messages further if there is no connection or
(3) vice verse it can stop sending messages if there is connection (don't know who would need this though)))

URL is optional parameter. You may choose particular server or IP address with or without port number.

It's published on npm

Enjoy)

Dave C-J

unread,
Mar 12, 2016, 4:30:30 PM3/12/16
to node...@googlegroups.com
Thanks for contributing that.

Julian Knight

unread,
Mar 12, 2016, 7:46:42 PM3/12/16
to Node-RED
Thanks Alex.

Going to try that one as I have similar requirements.

Incidentally, the TCP node has the same problem. You can't catch the error generated if the target doesn't respond. Makes it pretty useless for checking if a port is alive or dead which I want to do with a number of devices running specific services.

Dave C-J

unread,
Mar 13, 2016, 8:03:50 AM3/13/16
to node...@googlegroups.com
Alex's node will accept :port on the url so it can also check other tcp ports... I think ...

Julian Knight

unread,
Mar 13, 2016, 1:40:18 PM3/13/16
to Node-RED
OK, so Alex's node works perfectly! I can now detect whether a number of services are on/off-line including some that didn't work with the TCP node, namely the MQTT service. Sadly doesn't work with Crashplan on my NAS (port 4343) but I'm probably missing something.

However! It has highlighted a problem with the Change node I'm afraid. Alex's node returns true/false which is reasonable. But the Change node doesn't have boolean as an option for the "Search for" element nor does it even have number (though at a push I could perhaps use a regex for that?). Oddly, the "Replace with" field DOES have number but not boolean.

I really don't want to have to add a switch node in as well as the change node for each of the tested services as that will significantly add to the number of nodes in that flow.

Any chance Nick or Dave that this could be made more consistent? I think it should have number and boolean added to the Search for field and boolean, JSON and timestamp added to the Replace with field.

I will also raise as a request on Github for NR itself.

Julian Knight

unread,
Mar 13, 2016, 1:54:58 PM3/13/16
to Node-RED
Hi Alex, again many thanks for your node, I'm already finding it very useful.

I do have a couple of requests though if you have time at some point.
  • Accept msg.url on the inbound msg, use this if the url in the node is blank
  • Accept msg.topic on the inbound msg and pass through to output msg. (Actually, I'm not sure whether it already does this. Sorry I should have checked first).
These would add some great flexibility to the node and allow an upstream function node for example to read any number of URL's from a list, fire a check to each one with a suitable topic so that a single Is Online followed by a single MQTT node would be all that is required to gather on/off-line information for a load of services.

I've already got a flow containing 5 is online nodes each with a change node after it setting the topic and trying (see my other email reply to Dave) to change the boolean to "Online" and "Offline" which I use in my MQTT setup. I've a load of other services I want to check already but it is going to get out of hand. That will grow even further soon as I've ordered some ESP WiFi devices.

Cheers, Julian.

On Saturday, 12 March 2016 20:44:50 UTC, Alexander Pivovarov wrote:

Toshi Bass

unread,
Mar 13, 2016, 3:24:44 PM3/13/16
to Node-RED
+1 currently realy usefull .. but Julian's comments are spot on, I would like to use it in exactly the same way as he describes.

Toshi

Julian Knight

unread,
Mar 13, 2016, 3:41:51 PM3/13/16
to Node-RED
THanks Toshi,

Note that I've also raised this in the Github issues log.

Julian Knight

unread,
Mar 13, 2016, 3:43:30 PM3/13/16
to Node-RED
Node-Red Github issue raised.

On Sunday, 13 March 2016 17:40:18 UTC, Julian Knight wrote:
...
However! It has highlighted a problem with the Change node I'm afraid. Alex's node returns true/false which is reasonable. But the Change node doesn't have boolean as an option for the "Search for" element nor does it even have number (though at a push I could perhaps use a regex for that?). Oddly, the "Replace with" field DOES have number but not boolean.
...
I will also raise as a request on Github for NR itself.
...

Alexander Pivovarov

unread,
Mar 14, 2016, 4:20:53 AM3/14/16
to Node-RED
Thanks all for comments. 
Regarding feature requests - I would like to clarify some details.

-ap



воскресенье, 13 марта 2016 г., 22:43:30 UTC+3 пользователь Julian Knight написал:

Alexander Pivovarov

unread,
Mar 14, 2016, 4:27:24 AM3/14/16
to Node-RED


However! It has highlighted a problem with the Change node I'm afraid. Alex's node returns true/false which is reasonable. But the Change node doesn't have boolean as an option for the "Search for" element nor does it even have number (though at a push I could perhaps use a regex for that?). Oddly, the "Replace with" field DOES have number but not boolean.


I need to check, if is online returns boolean value. It seems to me it could be a string...
I encountered the same issue with Function node and I just added couple of lines of code:
if (msg.payload == "true") msg.payload = true;
if (msg.payload == "false") msg.payload = false;

I wanted to have a deeper look at this issue, but forgot. Need to check.



Julian Knight

unread,
Mar 14, 2016, 4:50:39 AM3/14/16
to Node-RED
Hi Alex, the problem is actually the other way around, you are returning a boolean (which is fine) but the change node doesn't recognise it.

In fact, Nick has already committed a fix for the change node - well done Nick :-)

I could, of course, have handled everything with a function node but it is always nice to be able to do these things without writing error-prone code.

So to reiterate, your node works fine as it is. However, the couple of changes I suggested and added to your issues log would enable a much simpler flow when trying to do multiple checks together.

Nicks changes will come through to NR in the next official update or could be extracted manually if I don't want to wait.

Alexander Pivovarov

unread,
Mar 14, 2016, 3:21:28 PM3/14/16
to Node-RED
Hi all, 

I published new version of is online node. 

1. If msg.url is set it overrides optional URL value in the config. Please note: if you set msg.url you get the following warnings:  "Warning: msg properties can no longer override set node properties. See bit.ly/nr-override-msg-props". I didn't find a workaround how to avoid it. Any help would appreciate. I wanted to set msg.url if it's not set but I removed this to reduce number of warnings.

2. I added new action option "Always pass through". It doesn't block any messages and the result of is online check is in the msg.online property.

3. It is possible to change timeout (by default it is 1000 ms) and some small improvements. 


Ideas for future releases.

- Julian offers to add possibility to override outputs to any custom values similar to Change node (if I understand correctly). I am not sure if it's a good idea as it would repeat features of other node (Change) and I think it's better to keep nodes as simple as possible. Julian, could you suggest examples how do you like to use this possible feature.

- I found out that I can't use is online node in some cases. For example, I have a PLC and I found out that I can't check its status if there is active connections. I have a Node JS application that opens Modbus TCP connection and keeps it open. Probably, it's not a good practice to keep open connections, but I know that other applications do the same. So, I am looking for workaround. If you have any ideas - welcome.

- Add more attempts to connect in case of failure. In the present version if the connection request fails it response offline status. Probably, it would be useful to add 2 more attempts, but I am not sure if this is actually necessary.

Kind regards,
Alex



Nicholas O'Leary

unread,
Mar 14, 2016, 3:40:15 PM3/14/16
to Node-RED Mailing List
Hi Alex,

if the url is set in the node it should not be possible for a message to override its value. If the user wants to provide a url in the message, they should leave the field blank in the node.

We found it was rare that a user would intentional set a value in the node AND want to be able to override it on a per-message basis. On the other hand, if a user didn't know a previous node had attached a url property to the message it would change the behaviour of the later node unexpectedly.

Nick

 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Julian Knight

unread,
Mar 14, 2016, 4:50:10 PM3/14/16
to Node-RED
Thanks Alex. I will give it a go as soon as I can.

Re my other suggestion, I immediately came across a use-case. In my MQTT messages for tracking whether devices and services are live, I used the strings "Online" and "Offline" as a clear designator. This rather than true/false since I want to be able to output the msg payload without further processing for human consumption. If you use true/false, you typically need to add an extra step when building a UI which isn't really necessary if you get the text right.

So when I use your node to test whether, for example, my MongoDB server is up and working, I want to send MQTT messages to something like DEVICES/NAS/MONGODB with a payload of "Online" or "Offline". All my other flows are based around that kind of structure.

So when your node returns true or false, I have to have an extra step in the flow:

INJECT (1 per minute) -->  IS-ONLINE --> CHANGE (true to Online and false to Offline) --> MQTT out

If you were to make the change, the flow becomes:

INJECT (1 per minute) -->  IS-ONLINE (output set to Online/Offline) --> MQTT out

Might not seem like much but not only is the 2nd flow easier to understand (important when you come back to several pages of complex flows after 6m or more) but it takes up less room on the display - again important when you have a great many flows.


It isn't the end of the world if you don't want to implement it, I will grab Nicks amended Change node. Without that, I would need to do something like:

INJECT (1 per minute) -->  IS-ONLINE --> SWITCH (true = Online and false = Offline) --> MQTT out

Which logic is even a little more complex.

Hope that clarifies.

Nicholas O'Leary

unread,
Mar 14, 2016, 4:53:11 PM3/14/16
to Node-RED Mailing List
Julian - or you could temporarily use a single Function node... no need to make it overly complex with Switches.

In general, I think the current approach of the node emitting a fixed well-known value for either state would be more consistent.

Nick

Julian Knight

unread,
Mar 14, 2016, 5:00:40 PM3/14/16
to Node-RED
Hi Nick, I could of course use a function node for all of that processing but that feels like giving in & I've too many function nodes already - doubtless most of them with subtle errors in them that will come back to bite me, usually when I'm away from home so they are hard to fix. 

As I say though, it isn't that big a deal now you've adjusted the Change node, just a little thing really.

Julian Knight

unread,
Mar 14, 2016, 5:52:42 PM3/14/16
to Node-RED
All seems to be working well thanks Alex. Though I am not getting a warning.


On Monday, 14 March 2016 19:21:28 UTC, Alexander Pivovarov wrote:

Julian Knight

unread,
Mar 14, 2016, 6:19:25 PM3/14/16
to Node-RED
I may have spoken too early.

I'm injecting to a function node that contains:

var checks = [
    {'topic':'SERVICES/NAS/CrashPlan', 'addr': '192.168.0.62:4343'},
    {'topic':'SERVICES/NAS/MongoDB', 'addr': '192.168.0.62:27017'},
    {'topic':'SERVICES/PI2/MongoDB', 'addr': '192.168.0.67:27017'},
    {'topic':'SERVICES/PI2/MQTT-sock', 'addr': '192.168.0.67:9001'},
    {'topic':'SERVICES/PI2/MQTT', 'addr': '192.168.0.67:1883'},
    {'topic':'SERVICES/ROUTER/Web', 'addr': '192.168.0.1'},
];

for ( var i in checks ) {
    node.send({
        'topic': i.topic,
        'url':   i.addr
    });
}

This sends to a single is-online node, from there to a function node that replaces true/false with on/offline and then out to a debug node (set to show the whole msg).

But neither the topic nor the url make it though to the other end. Typical output is:
14/03/2016, 22:16:152155f29c.f5efbemsg : Object{ "_msgid": "da728c62.258d7", "timestamp": 1457993776907, "online": true, "payload": "Online" }

Which is inconsistent with putting the url in the node and only passing in the topic. It also means that it isn't possible to see which url failed.

Even worse, all of the responses come back as true even if an invalid url is put in.

So I'm afraid it isn't working, sorry.
Message has been deleted
Message has been deleted

Alexander Pivovarov

unread,
Mar 14, 2016, 7:25:38 PM3/14/16
to Node-RED
Try this... )))

var checks = [
    {'topic':'SERVICES/NAS/CrashPlan', 'addr': '192.168.2.64:8000'},
    {'topic':'SERVICES/NAS/MongoDB', 'addr': '192.168.0.62:27017'},
    {'topic':'SERVICES/PI2/MongoDB', 'addr': '192.168.0.67:27017'},
    {'topic':'SERVICES/PI2/MQTT-sock', 'addr': '192.168.0.67:9001'},
    {'topic':'SERVICES/PI2/MQTT', 'addr': '192.168.0.67:1883'},
    {'topic':'SERVICES/ROUTER/Web', 'addr': '192.168.0.1'},
];

for ( var i in checks ) {
    node.send({
        'topic': checks[i].topic,
        'url':   checks[i].addr
    });
}


Julian Knight

unread,
Mar 15, 2016, 2:21:13 PM3/15/16
to Node-RED
Hey, thanks!

Just when you think you know JavaScript, it catches you out. Every other language would have done the sensible thing :-}

OK, working great now thanks.

Didn't have time to mess with getting the amended change node in the end so used a function node for now. Will replace on the next NR update.

I've now reduced 17 nodes in a flow down to 5! Result. That is going to get even more extreme a saving since I'm going to be adding a load more services to check as well. Probably including some external ones like key parts of my 2 VPS's, external mail servers and who knows what else.

Next step will be to put the list of checks out to an external store to make it easier to manage. 

If I can find the time, I might suggest a change to the ping node to take similar inputs as that is also rather simplistic.

Alexander Pivovarov

unread,
Mar 15, 2016, 2:55:52 PM3/15/16
to Node-RED
I created a config file and read data from it with file and json nodes.
[{"id":"94cde2c3.6b322","type":"inject","z":"c3db241.f3c24d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":98,"y":79,"wires":[["5a1e4e16.a5e1b"]]},{"id":"5a1e4e16.a5e1b","type":"file in","z":"c3db241.f3c24d8","name":"Config","filename":"smarthome-node-red-config.json","format":"utf8","x":246,"y":79,"wires":[["1f8f617d.e0709f"]]},{"id":"1f8f617d.e0709f","type":"json","z":"c3db241.f3c24d8","name":"","x":294,"y":138,"wires":[["5285886b.ad7a78"]]}]

2. Regarding ping node... I agree. Actually I was going to write such node, but found that there is already ping node in the library)))


вторник, 15 марта 2016 г., 21:21:13 UTC+3 пользователь Julian Knight написал:

Dave C-J

unread,
Mar 15, 2016, 3:52:19 PM3/15/16
to node...@googlegroups.com
... and pull requests are welcome :-)
(as long as they don't break the existing functionality)

Alexander Pivovarov

unread,
Mar 15, 2016, 4:26:32 PM3/15/16
to Node-RED


вторник, 15 марта 2016 г., 22:52:19 UTC+3 пользователь Dave C-J написал:
... and pull requests are welcome :-)
(as long as they don't break the existing functionality)

Ping node has no input right now. To implement these features it's necessary to add input to it.
Is it ok if a node has both input and output and only output is used?

-ap

Dave C-J

unread,
Mar 15, 2016, 4:29:44 PM3/15/16
to node...@googlegroups.com
hmmm - good point... forgot that...

Dave C-J

unread,
Mar 15, 2016, 4:36:11 PM3/15/16
to node...@googlegroups.com

Julian Knight

unread,
Mar 15, 2016, 6:46:13 PM3/15/16
to Node-RED
OK, so nobody likes a smart-a****se ;-)

I blame it on my age - again!

But yes, that seems to do what we want.

Toshi Bass

unread,
Aug 1, 2016, 2:00:55 PM8/1/16
to Node-RED
I am using "is online" node to check if servers are on line, fine it works well.....
(example) using function node with .....

var checks = [

    {'topic':'Webiopi   89 off-line', 'addr': '192.168.0.89:8000'},    
    {'topic':'Camera   89 off-line', 'addr': '192.168.0.89:8001'},
];

for ( var i in checks ) {
    node.send({
        'topic': checks[i].topic,
        'url':   checks[i].addr
    });
}

however I need to check if some ESP8266's are online in a similar manner but adding line {'topic':'ESP180 off-line', 'addr': '192.168.0.180'}, does not work with "is online" node, I am guessing its because there not servers ?

So I try "advanced ping" node ok so "function" node with...

msg.host = "192.168.0.180";
return msg;

works perfectly well give ping return time or false if n/c but I have been banging my head to get a similar method as above so I can check multiple ESP's but I just cannot get it ....

var checks = [
    {'topic':'180 off-line', 'addr': "192.168.0.180"},    
    {'topic':'103 off-line', 'addr': "192.168.0.103"}
];

for ( var i in checks ) {
    node.send({
        //"topic" : checks[i].topic,
        "msg.host":(checks[i].addr),
    });

just returns   No host is specified. Either specify in node configuration or by passing in msg.host   help to save my sanity would be apreceated thanks.

Toshi Bass

unread,
Aug 2, 2016, 10:21:07 AM8/2/16
to Node-RED
OK finally cracked checking esp;s with "advanced ping" for anyone following....

"function" node

var checks = [
    {'topic':'esp180 off-line', 'addr': "192.168.0.180"},    
    {'topic':'esp103 off-line', 'addr': "192.168.0.103"},
    {'topic':'esp100 off-line', 'addr': "192.168.0.100"},
];

for ( var i in checks ) {
    node.send({
        "topic" : checks[i].topic,
        "host"  : checks[i].addr,
    });

then into "advanced ping" then to another "function" node  ....

if (msg.payload===false){
    msg.payload=msg._topic;
}else{
    msg=null;
}  
return msg;

So say esp100 is off line I get msg ... esp100 off-line   what I don't get is why I have to use    msg._topic    for it to work, but now I am past caring.

Alexander Pivovarov

unread,
Aug 2, 2016, 6:44:09 PM8/2/16
to Node-RED
isonline tries to open a socket to the host to check connection.
I have a PLC which supports Modbus TCP and I connect it using port 502.
So if I use address like 192.168.1.100:502 I can check if my PLC is online or not.
But.. This works only if there is no other connections. 
Usually, I have a web-app that keeps open connection to PLC so I can't use isonline. 
Ping or Advanced Ping is a good alternative in this case.

PS
You should use _topic, as topic will contain host address as described here


-ap




вторник, 2 августа 2016 г., 17:21:07 UTC+3 пользователь Toshi Bass написал:
Reply all
Reply to author
Forward
0 new messages