mtcute/docs/guide/advanced/workers.md
alina sireneva 05077aa640
Some checks failed
Build and deploy typedoc / build (push) Waiting to run
Build and deploy docs / build (push) Successful in 1m56s
Tests / test-deno (push) Successful in 1m56s
Tests / test-bun (push) Successful in 2m5s
Tests / test-node (node22) (push) Successful in 2m14s
Tests / test-node (node20) (push) Successful in 2m21s
Tests / test-node (node18) (push) Successful in 2m29s
Tests / test-web (chromium) (push) Successful in 2m21s
Tests / test-web (firefox) (push) Successful in 1m33s
Tests / lint (push) Failing after 7m0s
Tests / e2e (push) Has been skipped
Tests / e2e-deno (push) Has been skipped
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:50:23 +03:00

109 lines
3 KiB
Markdown

# 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:
```ts
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:
```ts
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`:
```ts
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:
```ts
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:
```ts
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](./treeshaking.md)):
```ts
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](https://github.com/mtcute/mtcute/blob/master/packages/web/src/worker.ts)/[Node.js](https://github.com/mtcute/mtcute/blob/master/packages/node/src/worker.ts)
for the platform-specific worker implementations, and use them as a reference to create your own worker implementation.