title: Node.js & Socket.io - Cannot read property 'on' of undefined
body: <p>I have run into an error when establishing a websocket connection using socket.io and node.js. Here is my environment:</p>
  
  <ul>
  <li>OS: Windows 7 </li>
  <li>Node: 6.9.2</li>
  <li>NPM: 4.0.5</li>
  <li>Packages:
  
  <ul>
  <li>"express": "^4.14.0"</li>
  <li>"socket.io": "1.7.1"</li>
  <li>"socket.io-client": "1.7.1"</li>
  <li>"ws": "1.1.1"</li>
  </ul></li>
  </ul>
  
  <p>When a websocket connection is attempted the following error is thrown:</p>
  
  <pre><code>TypeError: Cannot read property 'on' of undefined
      at PerMessageDeflate.decompress (C:\ng-project\node_modules\ws\lib\PerMessageDeflate.js:242:37)
      at Receiver.applyExtensions (C:\ng-project\node_modules\ws\lib\Receiver.js:362:54)
      at C:\ng-project\node_modules\ws\lib\Receiver.js:508:14
      at Receiver.flush (C:\ng-project\node_modules\ws\lib\Receiver.js:347:3)
      at Receiver.opcodes.1.finish (C:\ng-project\node_modules\ws\lib\Receiver.js:541:12)
      at Receiver.expectHandler (C:\ng-project\node_modules\ws\lib\Receiver.js:493:33)
      at Receiver.add (C:\ng-project\node_modules\ws\lib\Receiver.js:103:24)
      at Socket.realHandler (C:\ng-project\node_modules\ws\lib\WebSocket.js:825:20)
      at ZoneDelegate.invokeTask (C:\ng-project\node_modules\zone.js\dist\zone-node.js:265:35)
      at Zone.runTask (C:\ng-project\node_modules\zone.js\dist\zone-node.js:154:47)
      at Socket.ZoneTask.invoke (C:\ng-project\node_modules\zone.js\dist\zone-node.js:335:33)
      at emitOne (events.js:96:13)
      at Socket.emit (events.js:188:7)
      at readableAddChunk (_stream_readable.js:177:18)
      at Socket.Readable.push (_stream_readable.js:135:10)
      at TCP.onread (net.js:542:20)
  </code></pre>
  
  <p>Looking into the <code>ws</code> module I can see the line of code throwing the error:</p>
  
  <pre><code>this._inflate.on('error', onError).on('data', onData);
  </code></pre>
  
  <p><code>_inflate</code> is a zlib <code>InflateRaw</code> instance created by <code>zlib.createInflateRaw({...})</code> a few lines earlier. The problem is that the <code>InflateRaw#on</code> function returns <code>undefined</code> in my environment.</p>
  
  <p>I have tried installing alternate versions of Node to work around this without any luck... Altering the <code>ws</code> package is not an acceptable workaround for me.</p>
  
  <p><strong>EDIT 1:</strong>
  Adding source code which throws the error.</p>
  
  <pre><code>io.on('connection', (socket) =&gt; {
    // Listen for verification subscription requests
    log.debug("Established socket connection " + socket.id);
    socket.on('verify subscribe', (msg) =&gt; {
      log.debug("Recieved 'verify subscribe' message: " + JSON.stringify(msg));
      // Do stuff....
    });
  });
  </code></pre>
  
  <p>This code outputs the following log messages prior to the error:</p>
  
  <pre><code>[2016-12-30T19:08:09.219Z] DEBUG: Established socket connection -g9oZM6pEqpFyU_BAAAA
  [2016-12-30T19:08:09.275Z] DEBUG: Recieved 'verify subscribe' message: {"token":"short-ish.token","authorization":"long.auth-token"}
  </code></pre>