question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
conradcommented, Jan 24, 2020

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.

1reaction
pladariacommented, Oct 12, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: No valid WebSocket class provided · Issue #135 - GitHub
I am trying to add the reconnect websocket to my code, but the debug of the red node gives me the error "Error:...
Read more >
WebSocket connection to 'ws://localhost:9090/' failed: Error in ...
The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project. Provide a...
Read more >
reconnecting-websocket - UNPKG
7, THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ... 108, * Returns true if given...
Read more >
ASP.NET Core SignalR connection troubleshooting
This error is usually caused by a client using only the WebSockets transport but the WebSocket protocol isn't enabled on the server. Response ......
Read more >
Writing WebSocket servers - Web APIs | MDN
A WebSocket server is nothing more than an application listening on any port of a TCP server that follows a specific protocol.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found