Skip to main content

Connection Settings

The Connection class configures how MadelineProto connects to Telegram’s servers.

Overview

use danog\MadelineProto\Settings;
use danog\MadelineProto\Stream\MTProtoTransport\AbridgedStream;

$settings = new Settings;
$settings->getConnection()
    ->setTimeout(10.0)
    ->setRetry(true)
    ->setProtocol(AbridgedStream::class);

Properties

protocol
class-string
default:"AbridgedStream::class"
MTProto transport protocol to use
transport
class-string
default:"DefaultStream::class"
Transport layer (TCP, WebSocket, etc.)
proxy
array
default:"[]"
Proxy configuration
obfuscated
bool
default:"false"
Whether to use obfuscation (helps bypass ISP blocks)
testMode
bool
default:"false"
Whether to connect to test servers
ipv6
bool
default:"false"
Whether to prefer IPv6
timeout
float
default:"5.0"
Connection timeout in seconds
pingInterval
int
default:"60"
Ping interval in seconds
retry
bool
default:"true"
Whether to retry failed connections
useDoH
bool
default:"true"
Whether to use DNS over HTTPS
bindTo
string|null
default:"null"
Bind to specific address and port
maxMediaSocketCount
int
default:"10"
Maximum number of media sockets
robinPeriod
int
default:"10"
Round-robin period in seconds

Protocol Configuration

setProtocol

Set the MTProto transport protocol.
protocol
class-string
required
Protocol class name
return
self
Returns self for method chaining
Available protocols: Lightest protocol with minimal overhead.
use danog\MadelineProto\Stream\MTProtoTransport\AbridgedStream;

$settings->getConnection()->setProtocol(AbridgedStream::class);
  • Overhead: Very small
  • Envelope: 1-4 bytes

IntermediateStream

Standard protocol with small overhead.
use danog\MadelineProto\Stream\MTProtoTransport\IntermediateStream;

$settings->getConnection()->setProtocol(IntermediateStream::class);
  • Overhead: Small
  • Envelope: 4 bytes

IntermediatePaddedStream

Padded version for use with obfuscation.
use danog\MadelineProto\Stream\MTProtoTransport\IntermediatePaddedStream;

$settings->getConnection()
    ->setProtocol(IntermediatePaddedStream::class)
    ->setObfuscated(true);
  • Overhead: Small-medium
  • Envelope: Random length
  • Best for: Bypassing ISP blocks

FullStream

Basic MTProto protocol with CRC and sequence checks.
use danog\MadelineProto\Stream\MTProtoTransport\FullStream;

$settings->getConnection()->setProtocol(FullStream::class);
  • Overhead: Medium
  • Envelope: 12 bytes
  • Features: CRC32 integrity check, sequence numbers

HttpStream / HttpsStream

For browsers and restricted webhosts.
use danog\MadelineProto\Stream\MTProtoTransport\HttpsStream;

$settings->getConnection()->setProtocol(HttpsStream::class);
  • Overhead: High
  • Use case: Web environments, additional TLS security

getProtocol

Get the current protocol.
return
string
Protocol class name
$protocol = $settings->getConnection()->getProtocol();

Transport Configuration

setTransport

Set the transport layer.
transport
class-string
required
Transport class name
return
self
Returns self for method chaining
use danog\MadelineProto\Stream\Transport\DefaultStream;

$settings->getConnection()->setTransport(DefaultStream::class);
Available transports:
  • DefaultStream - Standard TCP transport
  • WsStream - Plain WebSocket transport
  • WssStream - TLS WebSocket transport

getTransport

Get the current transport.
return
string
Transport class name
$transport = $settings->getConnection()->getTransport();

Connection Parameters

setTimeout

Set connection timeout.
timeout
float
required
Timeout in seconds
return
self
Returns self for method chaining
$settings->getConnection()->setTimeout(10.0);

getTimeout

Get connection timeout.
return
float
Timeout in seconds

setPingInterval

Set ping interval to keep connection alive.
pingInterval
int
required
Interval in seconds
return
self
Returns self for method chaining
$settings->getConnection()->setPingInterval(30);

getPingInterval

Get ping interval.
return
int
Interval in seconds

setRetry

Set whether to retry failed connections.
retry
bool
required
Enable/disable retry
return
self
Returns self for method chaining
$settings->getConnection()->setRetry(true);

getRetry

Get retry setting.
return
bool
Whether retry is enabled

Network Options

setIpv6

Enable IPv6 connections.
ipv6
bool
required
Enable/disable IPv6
return
self
Returns self for method chaining
$settings->getConnection()->setIpv6(true);

getIpv6

Get IPv6 setting.
return
bool
Whether IPv6 is enabled

setUseDoH

Enable DNS over HTTPS.
useDoH
bool
required
Enable/disable DoH
return
self
Returns self for method chaining
$settings->getConnection()->setUseDoH(true);

getUseDoH

Get DoH setting.
return
bool
Whether DoH is enabled

setBindTo

Bind connection to specific address.
bindTo
string|null
required
Address and port (e.g., “192.168.1.100:0”)
return
self
Returns self for method chaining
$settings->getConnection()->setBindTo('192.168.1.100:0');

getBindTo

Get bind address.
return
string|null
Bind address or null

Proxy Configuration

addProxy

Add a proxy to the connection.
proxy
class-string
required
Proxy class name
extra
array
default:"[]"
Proxy configuration (address, port, username, password)
return
self
Returns self for method chaining

SOCKS5 Proxy

use danog\MadelineProto\Stream\Proxy\SocksProxy;

$settings->getConnection()->addProxy(SocksProxy::class, [
    'address' => '127.0.0.1',
    'port' => 1080,
    'username' => 'user',
    'password' => 'pass',
]);

HTTP Proxy

use danog\MadelineProto\Stream\Proxy\HttpProxy;

$settings->getConnection()->addProxy(HttpProxy::class, [
    'address' => '127.0.0.1',
    'port' => 8080,
]);

setProxies

Set all proxies at once.
proxies
array
required
Array of proxy configurations
return
self
Returns self for method chaining
use danog\MadelineProto\Stream\Proxy\SocksProxy;

$settings->getConnection()->setProxies([
    SocksProxy::class => [
        ['address' => '127.0.0.1', 'port' => 1080],
    ],
]);

clearProxies

Remove all proxies.
return
self
Returns self for method chaining
$settings->getConnection()->clearProxies();

getProxies

Get all configured proxies.
return
array
Array of proxy configurations
$proxies = $settings->getConnection()->getProxies();

Obfuscation

setObfuscated

Enable connection obfuscation to bypass ISP blocks.
obfuscated
bool
required
Enable/disable obfuscation
return
self
Returns self for method chaining
use danog\MadelineProto\Stream\MTProtoTransport\IntermediatePaddedStream;

$settings->getConnection()
    ->setProtocol(IntermediatePaddedStream::class)
    ->setObfuscated(true);

getObfuscated

Get obfuscation setting.
return
bool
Whether obfuscation is enabled

Test Mode

setTestMode

Connect to Telegram test servers.
testMode
bool
required
Enable/disable test mode
return
self
Returns self for method chaining
$settings->getConnection()->setTestMode(true);
Test mode connects to separate Telegram test servers. Use only for development.

getTestMode

Get test mode setting.
return
bool
Whether test mode is enabled

Advanced Settings

setMaxMediaSocketCount

Set maximum number of media download sockets.
maxMediaSocketCount
int
required
Maximum socket count
return
self
Returns self for method chaining
$settings->getConnection()->setMaxMediaSocketCount(20);

setRobinPeriod

Set round-robin period for connections.
robinPeriod
int
required
Period in seconds
return
self
Returns self for method chaining
$settings->getConnection()->setRobinPeriod(10);

Complete Example

use danog\MadelineProto\Settings;
use danog\MadelineProto\Stream\MTProtoTransport\{AbridgedStream, IntermediatePaddedStream};
use danog\MadelineProto\Stream\Proxy\SocksProxy;

$settings = new Settings;

// Basic configuration
$settings->getConnection()
    ->setTimeout(10.0)
    ->setRetry(true)
    ->setPingInterval(60);

// Use obfuscation to bypass blocks
$settings->getConnection()
    ->setProtocol(IntermediatePaddedStream::class)
    ->setObfuscated(true);

// Configure SOCKS5 proxy
$settings->getConnection()->addProxy(SocksProxy::class, [
    'address' => '127.0.0.1',
    'port' => 9050, // Tor default port
]);

// Enable IPv6
$settings->getConnection()->setIpv6(true);

// Increase media download speed
$settings->getConnection()->setMaxMediaSocketCount(20);

See Also

Build docs developers (and LLMs) love