pladaria / reconnecting-websocket

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

No valid WebSocket class provided error when using "ws" directly #84

Closed bennycode closed 6 years ago

bennycode commented 6 years ago

My application is written in TypeScript and I am using reconnecting-websocket v3.2.2 in combination with the html5-websocket v2.0.4 interface. My goal is to run my application in a Node.js environment or a Browser environment and this is currently working with the following piece of code:

import * as Html5WebSocket from 'html5-websocket';
const ReconnectingWebsocket = require('reconnecting-websocket');

const RECONNECTING_OPTIONS = {
  connectionTimeout: 4000,
  constructor: typeof window !== 'undefined' ? WebSocket : Html5WebSocket,
  debug: false,
  maxReconnectionDelay: 10000,
  maxRetries: Infinity,
  minReconnectionDelay: 4000,
  reconnectionDelayGrowFactor: 1.3,
};

const socket = new ReconnectingWebsocket(
  () => this.buildWebSocketURL(),
  undefined,
  RECONNECTING_OPTIONS
);

Now I have seen that the "html5-websocket" is deprecated (since the "ws" package supports the export of a WebSocket interface) and that "reconnecting-websocket" got bumped to version 4. That's why I rewrote my code to the following:

import NodeWebSocket = require('ws');
const ReconnectingWebsocket = require('reconnecting-websocket');

const RECONNECTING_OPTIONS = {
  connectionTimeout: 4000,
  constructor: typeof window !== 'undefined' ? WebSocket : NodeWebSocket,
  debug: false,
  maxReconnectionDelay: 10000,
  maxRetries: Infinity,
  minReconnectionDelay: 4000,
  reconnectionDelayGrowFactor: 1.3,
};

const socket = new ReconnectingWebsocket(
  () => this.buildWebSocketURL(),
  undefined,
  RECONNECTING_OPTIONS
);

Compilation works just fine but when I want to test my code I get the following error from "reconnecting-websocket" v4.1.5 when running in a Node.js environment:

Error: No valid WebSocket class provided

It doesn't seem to be happy with the NodeWebSocket interface. What can I do about this? I am using "ws" v6.1.0.

jlarmstrongiv commented 6 years ago

I also ran into this issue, and found a similar one here. It turns out removing "module": "./dist/reconnecting-websocket.mjs", in the package.json fixes the issue in create-react-app.

pladaria commented 6 years ago

Hi @bennyn,

Version 4 of this library was a full rewrite with breaking changes. To pass a custom WebSocket class you must now use WebSocket option (not constructor). You can have a look at the test suite for examples.

I've just updated dependencies and tests run with ws@^6.1.0 without problems.

Let me know if you find any other issue.

pladaria commented 6 years ago

@jlarmstrongiv, I think your issue is unrelated. Can you please open a new issue providing more context?

conrad commented 4 years ago

I'm having this issue in NodeJS with reconnecting-websocket v4.2.0, ws v7.2.0, and node v10.15.0

The global value for WebSocket getting passed to isWebSocket is undefined.

conrad commented 4 years ago

Should I open a new issue?

shamoons commented 4 years ago

Any resolution?