mtcute/docs/guide/advanced/workers.md
alina sireneva dc08d93d2b
Some checks failed
Tests / e2e (push) Blocked by required conditions
Tests / e2e-deno (push) Blocked by required conditions
Tests / test-deno (push) Successful in 1m53s
Tests / test-bun (push) Successful in 2m1s
Tests / test-node (node22) (push) Successful in 2m8s
Tests / test-node (node20) (push) Successful in 2m10s
Tests / test-node (node18) (push) Successful in 2m16s
Tests / test-web (chromium) (push) Successful in 2m18s
Tests / test-web (firefox) (push) Successful in 2m31s
Tests / lint (push) Has been cancelled
Build and deploy typedoc / build (push) Has been cancelled
chore: moved docs inside the main repo
Co-authored-by: Kamilla 'ova <me@kamillaova.dev>
Co-authored-by: Alina Chebakova <chebakov05@gmail.com>
Co-authored-by: Kravets <57632712+kravetsone@users.noreply.github.com>
Co-authored-by: starkow <hello@starkow.dev>
Co-authored-by: sireneva <150665887+sireneva@users.noreply.github.com>
2025-01-17 08:44:07 +03:00

3 KiB

Workers

To facilitate parallel processing and avoid blocking the main thread, mtcute supports extractnig the heavy lifting to the workers. This is especially useful in the browser, where the main thread is often busy with rendering and other tasks.

::: warning Workers support is still experimental and may have some rough edges. If something doesn't work in a worker, but works when used directly, please open an issue. :::

Browser

@mtcute/web package exports a TelegramWorker and TelegramWorkerPort classes, which can be used to create workers and communicate with them.

To create a worker, use the TelegramWorker class:

import { BaseTelegramClient, TelegramWorker } from '@mtcute/web'

const tg = new BaseTelegramClient({
    apiId: 123456,
    apiHash: '...',
})

new TelegramWorker({
    client: tg,
})

To communicate with the worker, use the TelegramWorkerPort class and pass an instance of Worker (or SharedWorker) to it:

import { TelegramWorkerPort } from '@mtcute/web'

const port = new TelegramWorkerPort({
    worker: new Worker(
        new URL('./worker.ts', import.meta.url), 
        { type: 'module' },
    })
})

Node.js

On the surface, the API is largely the same, but is slightly different under the hood and uses worker_threads instead of web workers.

The worker is created the same way, but using TelegramWorker class from @mtcute/node:

import { BaseTelegramClient, TelegramWorker } from '@mtcute/node'

const tg = new BaseTelegramClient({
    apiId: 123456,
    apiHash: '...',
})

new TelegramWorker({
    client: tg,
})

Then, to communicate with the worker, use the TelegramWorkerPort class and pass an instance of Worker to it:

import { Worker } from 'worker_threads'
import { TelegramWorkerPort } from '@mtcute/node'

const port = new TelegramWorkerPort({
    worker: new Worker(
        new URL('./worker.js', import.meta.url), 
        { type: 'module' },
    ),
})

Usage

TelegramWorkerPort is a drop-in replacement for BaseTelegramClient, and since it implements ITelegramClient, you can pass it to any method that expects a client:

import { sendText } from '@mtcute/web/methods.js'

await sendText(port, 'me', 'Hello from worker!')

Alternatively, you can pass the port as a cliant to TelegramClient to bind it to all methods (not recommended in browser, see Tree-shaking):

const tg = new TelegramClient({ client: port })

await tg.sendText('me', 'Hello from worker!')

Other runtimes

In other runtimes it may also make sense to use workers. If your runtime supports web workers, you can use the @mtcute/web package to create workers - it should work just fine.

Otherwise, Please refer to Web/Node.js for the platform-specific worker implementations, and use them as a reference to create your own worker implementation.