Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
 /home/pankaj/node_modules/apns/lib/connection.js:193
  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: "gateway.sandbox.push.apple.com",
       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);

  connection.sendNotification(notification);
}

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

Please advice how to resolve this.

Thanks in advance.

like image 466
Curious101 Avatar asked Mar 15 '23 20:03

Curious101


1 Answers

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

The correct one is this: https://github.com/argon/node-apn

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

like image 65
eldelbar Avatar answered Apr 24 '23 19:04

eldelbar