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.

WebSocketManager disconnects 30-60 minutes at a time

See original GitHub issue

Which package is this bug report for?

ws

Issue description

Running WebSocketManager for a bot on my own personal Discord server

Tested this locally and on a staging within an EC2 instance – the same issue applies

Randomly throughout the day this will occur and could go on for upwards of 30-60 minutes.

How can I ensure no/minimal disconnection? (Code sample below – very straightforward implementation)

Connecting to [wss://gateway.discord.gg?v=10&encoding=json](wss://gateway.discord.gg/?v=10&encoding=json)
info:    Debug
Waiting for event hello for 60000ms
info:    Heartbeat
info:    Debug
Starting to heartbeat every 41250ms
info:    Debug
Identifying
        shard id: 0
        shard count: 1
        intents: 34305
        compression: none
info:    Debug
Disconnected normally from code 1000
info:    Debug
Destroying shard
        Reason: Got disconnected by Discord
        Code: 1000
        Recover: Reconnect
info:    Debug
Connecting to [wss://gateway.discord.gg?v=10&encoding=json](wss://gateway.discord.gg/?v=10&encoding=json)
info:    Debug
Waiting for event hello for 60000ms
info:    Heartbeat
info:    Debug
Starting to heartbeat every 41250ms
info:    Debug
Identifying
        shard id: 0
        shard count: 1
        intents: 34305
        compression: none
info:    Debug
Disconnected normally from code 1000
info:    Debug
Destroying shard
        Reason: Got disconnected by Discord
        Code: 1000
        Recover: Reconnect
info:    Debug
Connecting to [wss://gateway.discord.gg?v=10&encoding=json](wss://gateway.discord.gg/?v=10&encoding=json)
info:    Debug
Waiting for event hello for 60000ms
info:    Heartbeat
info:    Debug
Starting to heartbeat every 41250ms
info:    Debug
Identifying
        shard id: 0
        shard count: 1
        intents: 34305
        compression: none
info:    Debug
Disconnected normally from code 1000
info:    Debug
Destroying shard
        Reason: Got disconnected by Discord
        Code: 1000
        Recover: Reconnect
info:    Debug
Connecting to [wss://gateway.discord.gg?v=10&encoding=json](wss://gateway.discord.gg/?v=10&encoding=json)
info:    Debug
Waiting for event hello for 60000ms
info:    Heartbeat
info:    Debug
Starting to heartbeat every 41250ms
info:    Debug
Identifying
        shard id: 0
        shard count: 1
        intents: 34305
        compression: none

Code sample

if (!authToken) throw Error('BOT AuthToken not set');

  const rest = new REST().setToken(authToken);

  const manager = new WebSocketManager({
    handshakeTimeout: 20000,
    token: authToken,
    intents,
    rest,
  });

  manager.setStrategy(new SimpleShardingStrategy(manager));

  manager.addListener(WebSocketShardEvents.Debug, async (payload: { message: string }) =>
    logger.info(`Debug\n${payload.message}`)
  );

  manager.addListener(WebSocketShardEvents.Ready, () => logger.info('Ready'));

  manager.addListener(WebSocketShardEvents.Hello, () => logger.info('Heartbeat'));

  manager.addListener(WebSocketShardEvents.Resumed, () => logger.info('Resumed'));

  manager.addListener(
    WebSocketShardEvents.Dispatch,
    (
      event: {
        data: GatewayDispatchPayload;
      } & {
        shardId: number;
      }
    ) => logEvent(event.data)
  );

  await manager.connect();

Package version

14.3.0

Node.js version

18.6.0

Operating system

No response

Priority this issue should have

High (immediate attention needed)

Which partials do you have configured?

Message, Reaction

Which gateway intents are you subscribing to?

Guilds, GuildMessages, GuildMessageReactions, MessageContent

I have tested this issue on a development release

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
didinelecommented, Aug 27, 2022

packages/ws/src/ws/WebSocketShard.ts - mainly the onClose handler and the destroy method.

1reaction
didinelecommented, Nov 30, 2022

@didinele would this also alleviate issues related to #8486 / #4095 ?

discord.js (mainlib) does not currently use @discordjs/ws, it has its own own WS implementation (the “old” one), so no, those changes do not help at all at this time, but they will potentially will in the future when the main library starts using the ws package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is 3060 Minutes In Hours? (3060 min to hr)
To convert 3060 minutes into hours we have to multiply 3060 by the conversion factor in order to get the amount from minutes...
Read more >
3060 minutes to hours - Unit Converter
This conversion of 3,060 minutes to hours has been calculated by multiplying 3,060 minutes by 0.0166 and the result is 51 hours.
Read more >
3060 Minutes to Hours
3060 minutes = 51 hours and 0 minutes.​​ This alternative conversion is way more common in daily life!
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