node-apn: Provider does not shutdown after a successful send
I am trying to get a node.js process to terminate after sending and I have tried it on both AWS Lambda and locally on a variety of different node versions. It does not work unless the push token was invalid. It seems that it completely ignores some part of the shutdown otherwise if a token is actually sent.
const apn = require('apn');
var options_dev = {
token: {
key: "keys/key.p8",
keyId: "XXXXXX",
teamId: "XXXXXX"
},
production: false
};
let apnProvider_dev = new apn.Provider(options_dev);
let note = new apn.Notification();
note.badge = 1;
note.sound = "ping.aiff";
note.alert = "My Test Notification";
note.topic = "XXX";
apnProvider_dev.send(note, ["fff902d461d1212f0c267ff1ca3a5b0cc9676fd0372710cc3eef76dfed9f2e3f"]).then( function (result)
{
console.log('Done');
});
apnProvider_dev.shutdown();
If I intentionally modify the device token, everything shuts down and the process stops after shutdown. If however the push sends, the shutdown command seems to not actually stop anything and the process ends ~30 seconds later.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 1
- Comments: 24
Commits related to this issue
- fix(endpoint): destroy after shutdown to clear heartbeat interval (fixes #543) — committed to martijndeh/node-apn by martijndeh 7 years ago
- fix(endpoint): destroy after shutdown to clear heartbeat interval (fixes #543) — committed to martijndeh/node-apn by martijndeh 7 years ago
- fix(endpoint): destroy after shutdown to clear heartbeat interval (fixes #543) — committed to tisunov/node-apn by martijndeh 7 years ago
I am facing this problem too. I am simply using
node test.jsto run my script, and after getting the response, the process won’t exit immediately after callingshutdown().I had to do something like this to exit the process, but not sure if this is the right way to handle.
Any help would be appreciated!
So what I did for this was to make a promise, which waits for the send to finish before firing.
putting a .then here is a promise that is called once the the function is finished and gives me the result so I can use it in the next function.
This should properly shut down your apn.Provider after it sends.
I use @tri613 's solution and remove heart beat check
when using node-apn on aws lambda, I think _heartBeatIntervalCheck() can be removed.
AWS lambda is one time invocation and no need to keep connection, am I right?
I have the same issue, it can be fixed by adding the line
clearInterval(this._heartBeatIntervalCheck);at the beginning of the methodEndpoint.prototype.closein the file lib/protocol/endpoint.js.So the method will be:
I think it’s obvious because the same line is in the destroy method. So if we clear the interval in the destroy method why don’t we clear it in the close method?
I hope somebody creates a pull request.
Has there been any update on this? This is absolutely killing the ability to use this with AWS Lambda.
Hi, I’m having exactly the same issue as described by @DJHoltkamp, when a successful notification is sent the node process doesn’t complete when provider.shutdown() is called. However, if only a failed notification is sent then the process does complete when provider.shutdown() is called. The process finally completes after about 60s, causing timeouts on AWS Lambda, and I’ve experienced this on Lambda using node V4.3 and on my local environment using node V4.5
I’ve been using the promise as @DRSisco described, but still having this issue…
I’m looking into the library to see if I can find the problem, tried @DJHoltkamp solution with heartBeatIntervalCheck, but no luck so far, any other suggestions?
Ok, so possibly a better solution is to stop the timer after the connection closes
As I said, I am not experienced with Node.js (or this library). Is this an ok change to make? One thing to note is that the Endpoint.destroy() never gets called under my use case now, but the process does finish quickly as it should