pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.22k stars 197 forks source link

send() succeeds silently when underlying websocket is not initialized yet #67

Closed haizz closed 6 years ago

haizz commented 6 years ago

https://github.com/pladaria/reconnecting-websocket/blob/9b3eaee429253eb92d0f7e6aec38ac953ef681a3/reconnecting-websocket.ts#L220

    /**
     * Enqueues the specified data to be transmitted to the server over the WebSocket connection
     */
    public send(data: string | ArrayBuffer | Blob | ArrayBufferView) {
        if (this._ws) {
            this._ws.send(data);
        }
    }

Result: message is lost, but application code thinks everything is OK.

Real-life example of such occurrence:

const socket = new ReconnectingWebSocket(url);
// The main thread continues execution before ReconnectingWebSocket._ws is initialized.
// Maybe because of setTimeout() and Promises in ReconnectingWebSocket._connect logic?
socket.send("MSG");
// No exceptions, all send() calls are lost before _ws is initialized
haizz commented 6 years ago

I'm sorry, but because of this and #60 there is no way this supposedly very useful library is suited for any dependable production usage.

pladaria commented 6 years ago

Hi, sorry for the inconveniences. This and the other issue should be fixed in next release.

Thanks for reporting!