distribute
Creates middleware that distributes updates across cores.
This function should be used in combination with the Bot
class. Create an instance of Bot
in a separate file. Let’s assume that this file is called worker
. This will define your actual bot logic.
You can now do
const bot = new Bot("");
// Deno:
bot.use(distribute(new URL("./worker.ts", import.meta.url)));
// Node:
bot.use(distribute(__dirname + "/worker"));
in a central place to use the bot worker in worker
and send updates to it.
Under the hood, distribute
will create several web workers (Deno) or worker threads (Node) using worker
. Updates are distributed among them in a round-robin fashion.
You can adjust the number of workers via count
in an options object which is passed as a second argument, i.e. distribute(specifier
. By default, 4 workers are created.
Type Parameters
C
C extends { update: { update_id: number }; me: UserFromGetMe }
Parameters
specifier
specifier: ModuleSpecifier
Module specifier to a file which creates a Bot
options
options?: { count?: number }
Further options to control the number of workers