Restart a the http response in the the "get chunks" function

I have a GPS in my car that produced a continious http response for position once connecton is set up. I use the "get chunks" function here Http request (open connection) made by @BartButenaers and it works well.

const request = require('request')
request(
{ method: 'GET'
, uri: 'http://192.168.80.121/arx/eventexport?end_date=keep'
, gzip: true
}
, function (error, response, body) {
console.log('Most probably the stream has been ended ...')
}
)
.on('data', function(data) {
// decompressed data as it is received
console.log('decompressed chunk arrived: ' + data)
node.send({payload: data});
})

But when I park my car and the GPS is turned off something happens at the remote server side and when the GPS starts the http response just keep sending the last location before I turned the power off.

The sollution is to "re-run" the "get chuks" function but what happens then is that the old http response keeps comming, and a new one is added...and after some time I have a "storm" of responses.

So I guess I need to close the connection before doing the re-run of the "get chuks" function. I´m not a programmer, but if I have examples etc I can make some code. So can anybody please tell me how to close the "get chunks" connection?

Hi @tnesheim,

If I understand correctly, you need to be able to stop the currently running stream in the following two circumstances:

  1. When a new message is injected into the function node (e.g. when you want to restart your stream).
  2. When your function node is stopped, e.g. due to a (re)deploy of your flows.

So you need to store your request instance, in order to be able to stop it afterwards.
Out of my head, I 'think' your need to do something like this:

const request = require('request')

var activeRequest = flow.get("active_request");

if (activeRequest) {
   // If another request is still active (i.e. an active data stream), then stop it before starting a new one
   activeRequest.abort();
}

// Start a new request to start data streaming
activeRequest = request(
{ method: 'GET'
, uri: 'http://192.168.80.121/arx/eventexport?end_date=keep'
, gzip: true
}
, function (error, response, body) {
console.log('Most probably the stream has been ended ...')
}
)
.on('data', function(data) {
// decompressed data as it is received
console.log('decompressed chunk arrived: ' + data)
node.send({payload: data});
})

// Store your active request in flow memory, to allow you to access it afterwards
flow.set("active_request", activeRequest)

And you should also stop the stream in the "On Stop" tabsheet of the function node:

var activeRequest = flow.get("active_request");

if (activeRequest) {
   // If another request is still active (i.e. an active data stream), then stop it to avoid it keeps running after a deploy
   activeRequest.abort();
}

Otherwise your stream will running after a (re)deploy.

I have NOT tested this code!!

Good luck with it,
Bart

1 Like

Hi
Thanks for your reply and code suggestion. I look forward to test this! (currently I left for fall break, but back over the weekend)

1 Like