No valid WebSocket class provided error when using "ws" directly
See original GitHub issueMy 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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:6 (2 by maintainers)
I’m having this issue in NodeJS with
reconnecting-websocket
v4.2.0,ws
v7.2.0, andnode
v10.15.0The global value for
WebSocket
getting passed toisWebSocket
isundefined
.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 (notconstructor
). 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.