Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch error in ws.send when WebSocket is not in an open-state #12

Open
roerdinkholder opened this issue May 16, 2021 · 0 comments
Open

Comments

@roerdinkholder
Copy link

roerdinkholder commented May 16, 2021

When using this API, occassionally (~once a day), my application crashes due to the API trying to send over the websocket while the websocket is in a closed state:

\node_modules\ws\lib\websocket.js:329
  const err = new Error(

Error: WebSocket is not open: readyState 3 (CLOSED)
  at WebSocket.send (\node_modules\ws\lib\websocket.js:329:19)
  at doSendPublic (\node_modules\bitvavo\node-bitvavo-api.js:88:8)
  at Object.subscriptionTicker (\node_modules\bitvavo\node-bitvavo-api.js:857:9)

Due to the asynchronous nature of the ticker subscription, I cannot catch the thrown exception in my own code as far as I'm aware, so I'm a sitting duck for these crashes. The problem occurs in the following piece of code:

  const doSendPublic = function (ws, message) {
    debugToConsole('SENT: ' + message)
    ws.send(message)
  }

  const doSendPrivate = function (ws, message) {
    if (apiKey === '') {
      errorToConsole('You did not set the API key, but requested a private function.')
      return
    }
    if (this.authenticated) {
      debugToConsole('SENT: ' + message)
      ws.send(message)
    } else {
      waitForSocketPrivate(ws, () => {
        debugToConsole('SENT: ' + message)
        ws.send(message)
      })
    }
  }

The three ws.send commands should either pass a callback as third parameter, or be wrapped in a try-catch-block, to intercept the error. I'm willing to write a PR if you want. ws.send signature:

  /**
   * Send a data message.
   *
   * @param {*} data The message to send
   * @param {Object} options Options object
   * @param {Boolean} options.compress Specifies whether or not to compress `data`
   * @param {Boolean} options.binary Specifies whether `data` is binary or text
   * @param {Boolean} options.fin Specifies whether the fragment is the last one
   * @param {Boolean} options.mask Specifies whether or not to mask `data`
   * @param {Function} cb Callback which is executed when data is written out
   * @public
   */
  send(data, options, cb) {

This triggers the error in the ws.send method:

    if (this.readyState !== WebSocket.OPEN) {
      const err = new Error(
        `WebSocket is not open: readyState ${this.readyState} ` +
          `(${readyStates[this.readyState]})`
      );

      if (cb) return cb(err);
      throw err;
    }
@roerdinkholder roerdinkholder changed the title Catch error in ws.send when websocket is not open Catch error in ws.send when WebSocket is not in an open-state May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant