Watson IoT – Connection failure on subsequent posts

I am trying to publish a event to IoT Device on submitting a value using post request

From HTML page, I use JQuery Post method to submit a post request. I retrieve the post request in app.js using app.post method and the value in the request using Body-parser. I then connect to the IoT Service and publish a event

App Logs

> APP/PROC/WEB/0[BaseClient:connect]
> Connecting to IoTF with host :
> ssl://9z9mtd.messaging.internetofthings.ibmcloud.com:8883Jun
> 26, 2017 11:47:42.623 PM
> APP/PROC/WEB/03rd param is 12345Jun
> 26, 2017 11:47:42.623 PM
> APP/PROC/WEB/0Successfully connected
> to our IoT service!Jun 26, 2017
> 11:47:42.639 PM APP/PROC/WEB/0About to
> publish data Jun 26, 2017 11:47:42.691
> PM
> APP/PROC/WEB/0[ApplicationClient:connnect]
> ApplicationClient ConnectedJun 26,
> 2017 11:47:42.685 PM
> APP/PROC/WEB/0[ApplicationClient:publish]
> Publish:
> iot-2/type/CardSim/id/EB27FNW/evt/update/fmt/json,
> {“d”:{“eid”:”12345″,”loc”:1234}}, QoS
> : 0Jun 26, 2017 11:47:42.691 PM
> APP/PROC/WEB/0published data Jun 26,
> 2017 11:47:42.692 PM
> APP/PROC/WEB/0Successfully
> disconnected Jun 26, 2017 11:47:42.695
> PM APP/PROC/WEB/0[BaseClient:onClose]
> Connection was closed.Jun 26, 2017
> 11:47:42.705 PM
> APP/PROC/WEB/0[BaseClient:disconnect]
> Disconnected from the client.Jun 26,
> 2017 11:47:42.712 PM
>

Until now, Everything is fine.Now, If I update the value and press submit again, It publishes and crashes again

> APP/PROC/WEB/03rd param is 54321Jun
> 26, 2017 11:49:07.999 PM
> APP/PROC/WEB/0Successfully connected
> to our IoT service!Jun 26, 2017
> 11:49:08.004 PM
> APP/PROC/WEB/0[BaseClient:connect]
> Connecting to IoTF with host :
> ssl://9z9mtd.messaging.internetofthings.ibmcloud.com:8883Jun
> 26, 2017 11:49:08.001 PM
> APP/PROC/WEB/0[ApplicationClient:connnect]
> ApplicationClient ConnectedJun 26,
> 2017 11:49:08.032 PM
> APP/PROC/WEB/0[ApplicationClient:publish]
> Publish:
> iot-2/type/CardSim/id/EB27FNW/evt/update/fmt/json,
> {“d”:{“eid”:”12345″,”loc”:1234}}, QoS
> : 0Jun 26, 2017 11:49:08.032 PM
> APP/PROC/WEB/0About to publish data
> Jun 26, 2017 11:49:08.032 PM
> APP/PROC/WEB/0published data Jun 26,
> 2017 11:49:08.032 PM
> APP/PROC/WEB/0Successfully
> disconnected Jun 26, 2017 11:49:08.032
> PM
> APP/PROC/WEB/0[ApplicationClient:publish]
> Client is not connectedJun 26, 2017
> 11:49:08.032 PM APP/PROC/WEB/0 ^Jun
> 26, 2017 11:49:08.036 PM
> APP/PROC/WEB/0 at
> ApplicationClient.emit
> (events.js:163:17)Jun 26, 2017
> 11:49:08.036 PM

app.js Code(Post portion)

app.post(‘/status/data’, function(req, res) {
console.log(“3rd param is ” + req.body.eid);
var eventData = {
d:{
“eid” : req.body.eid,
“loc” : 1234
}
};

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 “);
application.disconnect();
console.log(“Successfully disconnected “);
});
});

Post request

function refresh() {
$.post( “/status/data”, { eid: $(“#entryID”).val()} );
}

Related:

Leave a Reply