mtcute/docs/guide/topics/transport.md
alina sireneva cfb572aefb
Some checks failed
Tests / e2e (push) Blocked by required conditions
Tests / e2e-deno (push) Blocked by required conditions
Build and deploy typedoc / build (push) Waiting to run
Build and deploy docs / build (push) Failing after 46s
Tests / lint (push) Has been cancelled
Tests / test-node (node18) (push) Has been cancelled
Tests / test-bun (push) Has been cancelled
Tests / test-node (node22) (push) Has been cancelled
Tests / test-web (chromium) (push) Has been cancelled
Tests / test-deno (push) Has been cancelled
Tests / test-node (node20) (push) Has been cancelled
Tests / test-web (firefox) (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:48:40 +03:00

137 lines
3 KiB
Markdown
Executable file

# Transport
Transport is a way for mtcute to communicate with Telegram servers.
mtcute comes bundled with TCP and WebSocket transport, and also
supports proxies via additional packages.
## TCP transport
TCP transport is the default transport for Node.js, and is implemented
using `net.Socket` in `@mtcute/node`:
```ts{5}
import { TcpTransport } from '@mtcute/node'
const tg = new TelegramClient({
// ...
transport: () => new TcpTransport()
})
```
::: tip
In Node.js it is used automatically, you don't need to pass this explicitly
:::
## WebSocket transport
WebSocket transport is mostly used for the browser,
but can also be used in Node.js.
It is implemented in `@mtcute/web`:
```ts{5}
import { WebSocketTransport } from '@mtcute/web'
const tg = new TelegramClient({
// ...
transport: () => new WebSocketTransport()
})
```
::: tip
In browser, it is used automatically, you don't need to pass this explicitly
:::
## HTTP(s) Proxy transport
To access Telegram via HTTP(s) proxy, you can use
`HttpProxyTcpTransport`, which is provided
by `@mtcute/http-proxy` (Node.js only):
```bash
pnpm add @mtcute/http-proxy
```
```ts{5-8}
import { HttpProxyTcpTransport } from '@mtcute/http-proxy'
const tg = new TelegramClient({
// ...
transport: () => new HttpProxyTcpTransport({
host: '127.0.0.1',
port: 8080
})
})
```
## SOCKS4/5 Proxy transport
To access Telegram via SOCKS4/5 proxy, you can use
`SocksTcpTransport`, which is provided
by `@mtcute/socks-proxy` (Node.js only):
```bash
pnpm add @mtcute/socks-proxy
```
```ts{5-8}
import { SocksTcpTransport } from '@mtcute/socks-proxy'
const tg = new TelegramClient({
// ...
transport: () => new SocksTcpTransport({
host: '127.0.0.1',
port: 8080
})
})
```
## MTProxy transport
To access Telegram via MTProxy (MTProto proxy), you can use
`MtProxyTcpTransport`, which is provided by `@mtcute/mtproxy` (Node.js only):
```bash
pnpm add @mtcute/mtproxy
```
```ts{5-8}
import { MtProxyTcpTransport } from '@mtcute/mtproxy'
const tg = new TelegramClient({
// ...
transport: () => new MtProxyTcpTransport({
host: '127.0.0.1',
port: 8080,
secret: '0123456789abcdef0123456789abcdef'
})
})
```
::: tip
mtcute supports all kinds of MTProxies, including the newer ones
with Fake TLS ⚡️
:::
## Changing transport at runtime
It is possible to change transport at runtime. For example, this
could be used to change proxy used to connect to Telegram.
To change the transport, simply call `changeTransport`:
```ts
tg.changeTransport(() => new MtProxyTcpTransport({...}))
```
## Implementing custom transport
When targeting an environment which is not supported already,
you can implement a custom transport on your own. In fact, it is
much simpler than it sounds!
You can check out source code for the bundled transports
to get the basic idea
[here](https://github.com/mtcute/mtcute/tree/master/packages/core/src/network/transports),
and re-use any packet codecs that are included.