When I run my server from the command line via node server.js
it runs differently (specifically the push notifications work) than when i start it with an upstart script. How should I modify my upstart script to solve this problem?
Here's an example server where the push fails in upstart vs succeeding in the commandline
var apn = require('apn');
var options = {
passphrase:'certpassphrase'
};
var err = null;
var apnConnection = new apn.Connection(options);
apnConnection.on('error', function(error){
console.log("apscallback", 1, error);
});
apnConnection.on('transmitted', function(notification){
console.log("apscallback", 2);
console.log(notification);
});
apnConnection.on('timeout', function(){
console.log("apscallback", 3);
});
apnConnection.on('connected', function(){
console.log("apscallback", 4);
});
apnConnection.on('disconnected', function(){
console.log("apscallback", 5);
});
apnConnection.on('socketError', function(error){
console.log("apscallback", 6, error);
});
apnConnection.on('transmissionError', function(errorCode, notification){
console.log("apscallback", 7, errorCode);
});
apnConnection.on('cacheTooSmall', function(difference){
console.log("apscallback", 8);
});
function sendTestNotification() {
var deviceToken = 'mydevicetoken';
var myDevice = new apn.Device(deviceToken);
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 60; // Expires 1 hour from now.
// note.badge = 3;
// note.sound = "ping.aiff";
// note.alert = "\uD83D\uDCE7 \u2709 You have a new message";
note.payload = {'message': 'somemessage', 'requester': 7};
apnConnection.pushNotification(note, myDevice);
}
sendTestNotification();
http.listen(port, function(){
console.log('listening on *:', port);
});
and here's what my upstart script looks like:
#!upstart
description "My Server"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
chdir /home/user/my-server/
exec node /home/user/my-server/server.js >> /var/log/my-server.log 2>&1
The error I get is:
...
apscallback 4
apscallback 5
apscallback 1 [Error: Socket unusable after connection. Hint: You may be using a certificate for the wrong environment]
... (REPEATS)
You did set env NODE_ENV=production
in your upstart script (by default if this is not set APN will go against the test/sandbox environment if that variable is not set). So this will go against the production APN. Do you have the right certificate in place to do that (certificate for sandbox and production are different)?