Skip to main content

Starting on a single cluster or shard

Once your bot grows to a lot of servers, you will want to implement sharding, process clustering, or another form of scaling. When doing this you will notice that the API plugin will try to start on every shard, which will then cause a crash due to the port already being in use.

To alleviate this issue you can configure the API plugin to not automatically start, which then allows you to start the API only 1 shard instead.

To configure this, you will need to set the automaticallyConnect option to false within the api options in the ClientOptions:

const { SapphireClient } = require('@sapphire/framework');

const client = new SapphireClient({
api: {
automaticallyConnect: false
}
});

Once you have done this, you can then start the API on a single shard by using the connect method on the initialised Server. For example, you can do this in your shardReady listener:

const { Listener } = require('@sapphire/framework');

class UserShardEvent extends Listener {
run(id, unavailableGuilds) {
if (id === 0) {
this.container.server.connect();
}
}
}
module.exports = {
UserShardEvent
};