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.

Unhandled 'error' event: "WebSocket was closed before the connection was established" when client was connected and network goes down

See original GitHub issue

Which package is this bug report for?

discord.js

Issue description

Steps to reproduce:

  1. Create a very simple bot like the code sample below
  2. Let the bot connect to discord
  3. Destroy the network/internet connection of the machine running the script (e.g. unplug the (virtual) network cable)
  4. Wait until the bot tries to reconnect
  5. See the app crashes on an unhandled ‘error’ event
Error: WebSocket was closed before the connection was established
    at WebSocket.close (/home/peter/tmp/node_modules/ws/lib/websocket.js:285:14)
    at WebSocketShard.destroy (/home/peter/tmp/node_modules/discord.js/src/client/websocket/WebSocketShard.js:728:27)
    at Timeout.<anonymous> (/home/peter/tmp/node_modules/discord.js/src/client/websocket/WebSocketShard.js:522:12)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (/home/peter/tmp/node_modules/ws/lib/websocket.js:984:13)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Full log: log.txt

There seams to be no way to handle the WebSocket error event.

Expected behavior would be, that discord.js keeps trying to reestablish the connection, or that there is some chance to handle the error.

Came up with this, as my internet connection was gone away and my application using discord.js crashed with the above error.

Code sample

const { Client, Intents } = require('discord.js');

const token = 'xxx';

async function main () {
  const client = new Client({
    intents: [
      Intents.FLAGS.GUILDS,
    ],
  });

  client.on('debug', console.debug);
  client.on('warn', console.warn);
  client.on('error', console.error);

  client.on('ready', () => console.log('ready'));

  await client.login(token);
}

main();

Package version

13.7.0

Node.js version

16.14.2

Operating system

Debian 10

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds

I have tested this issue on a development release

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:26 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
crycode-decommented, May 24, 2022

@legendhimslef Using your PR #7581 seams to work fine. The error then gets emitted as shardError and a reconnect is queued. 👍

1reaction
iCrawlcommented, Aug 9, 2022

@pedroricardo Additionally to the thing above, please also have an error handler like I showed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Error: WebSocket was closed before the connection ...
It happens when you call close() on websocket when connection is not established. The code you include in the question does not include...
Read more >
error: websocket was closed before the connection ... - You.com
It happens when you call close() on websocket when connection is not established. The code you include in the question does not include...
Read more >
Error with WebSocket - Microsoft Q&A
Hello I get the error: WebSocket connection to 'wss://localhost:54970/Dashboard.Server/' failed: Error in connection establishment: net:: ...
Read more >
WebSocket: error event - Web APIs | MDN
The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent...
Read more >
Why Am I Receiving "Websocket Is Closed Before ... - Pusher
The WebSocket is closed before the connection is established error message indicates that some client code, or other mechanism, has closed the ...
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