Search code examples
node.jsiotwatson-iot

Watson IoT & Node.JS - Continuous retry & connection failure


Background: I am creating a Node.js Web application to simulate a device & publish a event to Node-Red application & Device registered in Watson IoT Platform.

The Node red Application subscribes to the event and does custom logic.

As part of the Node.js web application, On click of submit button in Web Application, I am submitting a post request with a text box value. I am processing the request in app.js and publishing the event

Issue: It works perfectly fine, When I submit the request for the first time. But from the second time,It continuously tries to reconnect and repeatedly publish the event

App.js Code --> Post method portion

app.post('/status/data', function(req, res) {    console.log("3rd param is " + req.body.eid);      application.connect();   console.log("Successfully connected to our IoT service!");      application.on("connect", function () {         console.log("About to publish data ");        application.publishDeviceEvent(deviceType, deviceId, eventType, eventFormat, eventData);         console.log("published data ");    });     res.writeHead(200, "OK", {'Content-Type': 'text/plain'});   res.end();       }); 

First Time post request - Log Details

Jun 29, 2017 04:12:47.346 PM APP/PROC/WEB/0 3rd param is 111111 Jun 29, 2017 04:13:04.785 PM APP/PROC/WEB/0 [BaseClient:connect] Connecting to IoTF with host : ssl://9z9mtd.messaging.internetofthings.ibmcloud.com:8883 Jun 29, 2017 04:13:04.786 PM APP/PROC/WEB/0 Successfully connected to our IoT service! Jun 29, 2017 04:13:04.814 PM APP/PROC/WEB/0 About to publish data Jun 29, 2017 04:13:04.855 PM APP/PROC/WEB/0 [ApplicationClient:publish] Publish: iot-2/type/CardSim/id/EB27FNW/evt/update/fmt/json, {"d":{"eid":222261,"loc":1234}}, QoS : 0 Jun 29, 2017 04:13:04.855 PM APP/PROC/WEB/0 published data Jun 29, 2017 04:13:04.855 PM APP/PROC/WEB/0 [ApplicationClient:connnect] ApplicationClient Connected Jun 29, 2017 04:13:04.855 PM RTR/1

Second time POst Request - Log Details

PP/PROC/WEB/0 3rd param is 222222 Jun 29, 2017 04:13:52.891 PM APP/PROC/WEB/0 [BaseClient:connect] Connecting to IoTF with host : ssl://9z9mtd.messaging.internetofthings.ibmcloud.com:8883 Jun 29, 2017 04:13:52.891 PM RTR/0 cardreaderapp.mybluemix.net - [2017-06-29T10:43:52.857+0000] "POST /status/data HTTP/1.1" 200 10 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0" "108.168.250.151:18180" "169.47.199.84:63789" x_forwarded_for:"167.230.96.8" x_forwarded_proto:"https" vcap_request_id:"0801beea-5ab8-434b-403d-6c5cc92e9aad" response_time:0.050970748 app_id:"0cdbfec6-ac2a-4be1-9bab-3b1933ba3c57" app_index:"0" x_global_transaction_id:"1003954655" x_b3_traceid:"920cb5951fe2dee3" x_b3_spanid:"920cb5951fe2dee3" x_b3_parentspanid:"-" Jun 29, 2017 04:13:52.908 PM APP/PROC/WEB/0 Successfully connected to our IoT service! Jun 29, 2017 04:13:52.895 PM APP/PROC/WEB/0 [BaseClient:onClose] Connection was closed. Jun 29, 2017 04:13:52.929 PM APP/PROC/WEB/0 [BaseClient:connect] Iotfclient is offline. Retrying connection Jun 29, 2017 04:13:52.928 PM APP/PROC/WEB/0 [BaseClient:connect] Retry in 3 sec. Count : 1 Jun 29, 2017 04:13:52.928 PM APP/PROC/WEB/0 [ApplicationClient:connnect] ApplicationClient Connected Jun 29, 2017 04:13:52.930 PM APP/PROC/WEB/0 About to publish data Jun 29, 2017 04:13:53.972 PM APP/PROC/WEB/0 [ApplicationClient:publish] Publish: iot-2/type/CardSim/id/EB27FNW/evt/update/fmt/json, {"d":{"eid":222261,"loc":1234}}, QoS : 0

This process keeps repeating. Please help


Solution

  • For every POST to your application you are (re)connecting to IoTP and registering an on-connect() callback.

    Instead of doing it this way, you should connect and register any on-connect callbacks (not one that publishes POST data!) outside of the function that handles POST, like app init.

    In the POST function, the only thing you need to do is call publishDeviceEvent().

    Note publishDeviceEvent() is async of course, so its probably not actually published when the function returns. You can wait for it by passing in a callback, which then responds to the caller via res.end() having written the header.