Pankaj Pankaj - 1 year ago 140
Node.js Question

I'm trying to use Apns library for node.js and get an error - "TypeError: Cannot read property 'bufferSize' of undefined"

On Ubuntu Linux I am running node.js 0.12.7 and I have installed npm package "apns". I wish to send push notifications to apple devices and later on to Android and Windows.

I have very basic code but am still running into issues. The script throws following run time error:

pankaj@pankaj-Ubuntu-asus:~/nodeServer/testCode$ node testPushDirectToApple.js
[Wed Sep 09 2015 02:32:12 GMT-0400 (EDT)] Send : 6840038dee0ac6aa3c4a5210746961e80ed9797410a5100f8835b8dfbb
[Wed Sep 09 2015 02:32:12 GMT-0400 (EDT)] open socket
[Wed Sep 09 2015 02:32:12 GMT-0400 (EDT)] drain
if(this.tlsConnection && this.tlsConnection.socket.bufferSize !== 0)
TypeError: Cannot read property 'bufferSize' of undefined
at Connection.drain (/home/pankaj/node_modules/apns/lib /connection.js:193:54)
at TLSSocket.<anonymous> (/home/pankaj/node_modules/apns/lib/connection.js:171:12)
at TLSSocket.g (events.js:199:16)
at TLSSocket.emit (events.js:104:17)
at TLSSocket.<anonymous> (_tls_wrap.js:950:16)
at TLSSocket.emit (events.js:104:17)
at TLSSocket._finishInit (_tls_wrap.js:460:8)

Here is my code: "testPushDirectToApple.js"

var apns = require("apns"), notification, options, connection;

options = {
keyFile : "privateKey_key.pem",
certFile : "aps_development_cert.pem",
passphrase: "<scrubbed>",
gateway: "",
port: 2195,
debug : true

function pushviaDirectConenction(deviceToken, message){

connection = new apns.Connection(options);

notification = new apns.Notification();
notification.alert = message;
notification.device = new apns.Device(deviceToken);


pushviaDirectConenction("68c2440038dee0ac6aa3c4a5210746961e80ed9797410a5100f8835b8d", "Hi there");

Please advice how to resolve this.

Thanks in advance.

Answer Source

I had the same problem and I found out that I was using the wrong library.

The correct one is this:

Be careful because there is another one library called node-apns which I think is not continued and deprecated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download