-
Notifications
You must be signed in to change notification settings - Fork 3
Messaging
A Messaging system allows communication between different clusters or instances in a Galactic setup. This is particularly useful for sending commands, data, or events across multiple shards or clusters, enabling coordinated behavior in a distributed bot architecture.
The Bridge can send messages to specific clusters using the sendToCluster method. This allows the Bridge to communicate directly with any cluster it manages.
// Bridge
sendRequestToGuild(cluster: BridgeClientCluster, guildID: Snowflake, data: unknown, timeout = 5000): Promise<unknown>;
// Cluster
cluster.on('request', (message: unknown, resolve: (data: unknown) => void, reject: (error: any) => void) => {
// resolve({ ... });
// or
// reject({ ... });
})An Instance can send messages to its clusters using the sendRequestToClusterOfGuild method. This is useful for coordinating actions or sharing data between the instance and its clusters.
instance.sendRequestToClusterOfGuild(guildID: string, message: unknown, timeout?: number): Promise<unknown>You can also send messages without needing an guildID.
sendRequestToCluster(cluster: ClusterProcess, message: unknown, timeout?: number): Promise<unknown>;If you need to send messages between clusters, you can do so by routing the message through the Bridge or Instance.
sendRequestToClusterOfGuild(guildID: string, message: unknown, timeout?: number): Promise<unknown>;
sendMessageToClusterOfGuild(guildID: string, message: unknown): void;
Like discord.js's Client#broadcastEval, Galactic provides a way to evaluate code across all clusters or instances. This is useful for gathering information or executing commands in a distributed manner.
// Cluster
const guildCount = await client.cluster.broadcastEval((client) => {
return client.guilds.cache.size;
}).then((results) => {
return results;
}).catch((error) => {
return [] as number[];
});