#How to properly communicate between shards?

4 messages · Page 1 of 1 (latest)

buoyant anchor
#

I tried something like this, but it ended up sending hundreds of messages when it should have sent one, or otherwise just crashing out. I can't seem to find a good guide or example on how to set up a way for one shard to communicate something to all shards.

// Index.ts
manager.spawn()
    .then(shards => {
        shards.forEach(shard => {
            shard.on('message', message => {
                manager.broadcast(message);
            });
        });
    })
    .catch(console.error);
``````ts
// Bot.ts
process.on('message', (message: string) => {
    let data;
    try {
        data = JSON.parse(message);
    } catch {
        return;
        // Ignore
    }

    console.log(data.message);
});

/// On an event:
process.send(JSON.stringify({ message: 'Hello World!'}));
rigid parcelBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
hardy ridge
#

Did that process.on message by any chance emit the event that caused the process.send leading to a feedback loop?

buoyant anchor
#

Nope