Websocket with Redux
<p>I'm looking at the redux example using a websocket as found here:</p>

<p><a href="https://github.com/YannickDot/redux-websocket-example/blob/master/app/src/js/app.js" rel="nofollow">https://github.com/YannickDot/redux-websocket-example/blob/master/app/src/js/app.js</a></p>

<p>Here is the implementation:</p>

<pre><code>const sock = {
  ws: null,
  URL: 'echo.websocket.org',
  wsDipatcher: (msg) =&gt; {
    return redux.dispatch(ChatActions.receiveMessage(msg));
  },
  wsListener: () =&gt; {
    const { lastAction } = redux.getState();

    switch (lastAction.type) {
      case ActionTypes.POST_MESSAGE:
        return sock.ws.postMessage(lastAction.text);

      case ActionTypes.CONNECT:
        return sock.startWS();

      case ActionTypes.DISCONNECT:
        return sock.stopWS();

      default:
        return;
    }
  },
  stopWS: () =&gt; {
    sock.ws.close();
    sock.ws = null
  },
  startWS: () =&gt; {
    if(!!sock.ws) sock.ws.close();

    sock.ws = new WSInstance(sock.URL, sock.wsDipatcher)
  }
};
</code></pre>

<p>When using websockets you can't send until the socket has been opened, which is established using an onopen handler. Therefore if I understand correctly, the above code might not work if CONNECT and POST_MESSAGE events were dispatched in quick succession. How should this situation be handled? Should wsListener buffer data associated with POST_MESSAGE until onopen is called, with this handler the sending the buffered data.</p>