Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot switch to old mode now" - Node.JS apn module error in tls.connect function

I am trying to implement Node.JS apn module to connect to APNS (Apple Push Notification service) to push notification to iPhone devices from the Node server (using ExpressJS) hosted on Amazon EC2 instance running Ubuntu 12.04. I am getting this error:

"Error: Cannot switch to old mode now.",
"    at emitDataEvents (_stream_readable.js:720:11)",
"    at ReadStream.Readable.resume (_stream_readable.js:705:3)",
"    at TLSSocket.<anonymous> (/home/ubuntu/usemebeta/routes/message.js:101:48)",
"    at TLSSocket.g (events.js:186:14)",
"    at TLSSocket.EventEmitter.emit (events.js:97:17)",
"    at TLSSocket.<anonymous> (_tls_wrap.js:579:16)",
"    at TLSSocket.EventEmitter.emit (events.js:97:17)",
"    at TLSSocket._finishInit (_tls_wrap.js:198:8)"

when I call the apn module function to push notification. However when I do the same thing in a function written in a file and execute that file, then it works just fine. Please help. How should I go about fixing the problem?

like image 944
ajay Avatar asked Aug 19 '13 05:08

ajay


1 Answers

Update: The behavior described below may have changed since this answer was originally written; as of 0.12.3, the docs suggest that you can now switch back and forth between paused (new) and flowing (old) mode, and that streams start out in paused mode.
Also, "old" mistakenly suggests a deprecated way of doing things, but while the flowing mode came first, both are supported and have their uses.


I don't have a specific answer, but a general explanation:

The short of it: old code that calls .resume() or .pause() on readable streams can break on node 0.10 and higher.

The long of it: You see this error on node 0.10 or higher when a readable stream has been initialized to use the new, paused mode (based on the readable event) and an attempt is later made to switch to the old, flowing mode (based on the data event), which is implicitly attempted when you call .resume() or .pause() - see the docs.

Setting up a readable event may not even be directly involved. For instance, it seems that using the .pipe() method now implicitly switches to the new, paused mode, so a subsequent .resume() call would trigger this error.

like image 91
mklement0 Avatar answered Nov 14 '22 15:11

mklement0