faye/faye-websocket-node — Issue #13: Open event fired before handshake completed on draft76
Author: majek
Created: 2012-02-01 12:11:07+00:00
State: open

There is a bug, which happens only for draft76 behind haproxy. It was quite easy to spot it, but here it goes:

https://github.com/faye/faye-websocket-node/blob/master/lib/faye/websocket.js#L56

```
var event = new Event('open');
event.initEvent('open', false, false);
this.dispatchEvent(event);

var self = this;

this._stream.addListener('data', function(data) {
var response = self._parser.parse(data);
if (!response) return;
try { self._stream.write(response, 'binary') } catch (e) {}
});
```

After the websocket handler sends the `handshake`, it emits the `open` event. After that it listens to the `data` event on the stream. The problem is - handshake may not yet be completed. This can lead to emitting `open` event prematurely. During the users `open` handler, there may be some data emitted. This will lead to wrong ordering - user data will be send before the other part of the handshake. This will result in the connection being aborted from the browser (due to bad key).