Simon
Simon

Reputation: 5247

Cannot get node-apn to connect

I'm trying to make a connection to APNs. It simply won't connect. I get variations of:

  apn Socket error occurred +609ms { [Error: socket hang up] code: 'ECONNRESET' }

and

apn Connection error occurred before TLS Handshake +0ms

This is for a Passbook pass. Not an app. I'm using Passbook certificates.

My code is:

var apns = require('apn');

var root = process.cwd();

var fs = require('fs');

var options = {
    cert: root + '/certs/new/cert.pem',                 /* Certificate file path */
    certData: null,                   /* String or Buffer containing certificate data, if supplied uses this instead of cert file path */
    key:  root + '/certs/new/key.pem',                  /* Key file path */
    keyData: null,                    /* String or Buffer containing key data, as certData */
    passphrase: 'secret',                 /* A passphrase for the Key file */
    ca: null,                         /* String or Buffer of CA data to use for the TLS connection */
    gateway: 'gateway.sandbox.push.apple.com',/* gateway address */
    port: 2195,                       /* gateway port */
    enhanced: true,                   /* enable enhanced format */
    errorCallback: undefined,         /* Callback when error occurs function(err,notification) */
    cacheLength: 100                  /* Number of notifications to cache for error purposes */
};

var apnsConnection = new apns.Connection(options);

var myDevice = new apns.Device('token');

var note = new apns.Notification();

note.payload = {};
note.device = myDevice;

apnsConnection.sendNotification(note);

Upvotes: 5

Views: 5334

Answers (3)

Simon
Simon

Reputation: 5247

It appears that I mixed up my certificates. I'm sure I tried swapping them earlier but obviously didn't.


cert: Your app cert.
key: Apple's WWDR

Upvotes: 1

tolgaio
tolgaio

Reputation: 3246

Try the following structure : Read the .cert and .key files manually and set them as certData and keyData property, respectivelly. Here is the core :

var key = root + '/certs/new/key.pem'
var cert = root + '/certs/new/cert.pem';

var certData = fs.readFileSync(cert, encoding='ascii');
var keyData = fs.readFileSync(key, encoding='ascii');

var apnsConnection = new apns.Connection({
    certData: certData,
    keyData: keyData,
    gateway: 'gateway.sandbox.push.apple.com',
    port: 2195,
    ... /* other configs of course */
});

Upvotes: 0

hereandnow78
hereandnow78

Reputation: 14434

are you behind a proxy? that could be the issue (at least it is often in my case)

Upvotes: 0

Related Questions