title: SocketRocket crashes Node.js WebSocket
body: <p>I wrote a simple <a href="https://stackoverflow.com/a/19469286/242933">Node.js WebSocket chat server</a>. To run the server, I'm using <code>foreman start</code> with a <code>Procfile</code> that merely contains the line: <code>chat: npm start</code>.</p>
  
  <p>I also wrote an iPhone app that uses <a href="https://github.com/square/SocketRocket" rel="nofollow noreferrer">SocketRocket</a> to connect to the aforementioned server. In <code>applicationDidEnterBackground:</code>, I call <code>close</code> on <code>webSocket</code>. And, In <code>applicationWillEnterForeground:</code>, I recreate <code>webSocket</code> and call <code>open</code>.</p>
  
  <p>When entering the background or foreground, the iPhone app seems to crash the server with the error:</p>
  
  <pre><code>chat.1 | events.js:74
  chat.1 |         throw TypeError('Uncaught, unspecified "error" event.');
  chat.1 |               ^
  chat.1 | TypeError: Uncaught, unspecified "error" event.
  chat.1 |     at TypeError (&lt;anonymous&gt;)
  chat.1 |     at WebSocket.EventEmitter.emit (events.js:74:15)
  chat.1 |     at Receiver.self._receiver.onerror (~/Projects/Chat/node_modules/ws/lib/WebSocket.js:719:10)
  chat.1 |     at Receiver.error (~/Projects/Chat/node_modules/ws/lib/Receiver.js:301:8)
  chat.1 |     at Receiver.opcodes.8.finish (~/Projects/Chat/node_modules/ws/lib/Receiver.js:497:14)
  chat.1 |     at Receiver.&lt;anonymous&gt; (~/Projects/Chat/node_modules/ws/lib/Receiver.js:478:33)
  chat.1 |     at Receiver.add (~/Projects/Chat/node_modules/ws/lib/Receiver.js:93:24)
  chat.1 |     at Socket.firstHandler (~/Projects/Chat/node_modules/ws/lib/WebSocket.js:678:22)
  chat.1 |     at Socket.EventEmitter.emit (events.js:95:17)
  chat.1 |     at Socket.&lt;anonymous&gt; (_stream_readable.js:746:14)
  chat.1 | npm ERR! weird error 8
  chat.1 | npm ERR! not ok code 0
  chat.1 | exited with code 1
  system | sending SIGTERM to all processes
  </code></pre>
  
  <p>Why is this happening? And, how do I fix it?</p>