DEV Community

AmirHossein Kargar
AmirHossein Kargar

Posted on

How We Beat Telegram's Official Bot API Server in PHP (With Native WebSockets & 291k Updates/sec)

If you've ever scaled a high-traffic Telegram bot, you know the pain points of the official telegram-bot-api C++ server:

  1. You have to compile C++ from source (which takes forever and eats gigabytes of RAM during builds) just to unlock local file limits.
  2. Delivery is stuck in the HTTP era: You either deal with the latency of Long Polling or the HTTP overhead, SSL certs, and connection churn of Webhooks.
  3. Zero native WebSocket support: Want real-time bidirectional communication without spinning up an HTTP server for every bot? You're out of luck.

We built LaraGram APIServer (laraxgram/apiserver) to fix this. It is a drop-in replacement for the official Telegram Bot API server, written in PHP directly on top of LaraGram MTProto—and in real-world benchmarks, it actually outperforms the official C++ binary.


Why Replace the Official telegram-bot-api?

LaraGram APIServer speaks the exact same HTTP Bot API protocol your existing bots already use—meaning you can point any bot framework (LaraGram, GrammY, Aiogram, Telethon, node-telegram-bot-api, etc.) to your local endpoint without changing a single line of business logic.

On top of standard Bot API compatibility, it unlocks features the official server either restricts or lacks entirely:

  • Native Bidirectional WebSockets: Receive updates and invoke Bot API methods over a single persistent WebSocket connection—no public IP, no reverse proxy, and no HTTP handshake overhead per request.
  • 2,000 MB Uploads & Unlimited Downloads: Bypass Telegram's standard 50 MB / 20 MB cloud limits with local file access (file://) and full MTProto chunked transfers.
  • Compatible file_ids: Seamless file_id handling compatible with standard Bot API workflows.
  • Multi-Bot Hosting in One Process: Run and manage multiple bot tokens concurrently inside a single lightweight daemon (active_bot_count scaling cleanly without spawning separate C++ processes).
  • Zero C++ Compilation: Install globally via Composer (composer global require laraxgram/apiserver) and launch it from anywhere with a single command—powered out of the box by Surge (Swoole / Open Swoole / RoadRunner / FrankenPHP).
  • Language-Agnostic & Ecosystem-Ready: Works seamlessly with any Bot API client in any language—and if your bot is built with LaraGram v4, you also get native integration with Sentinel for real-time update debugging and queue metrics.

The Killer Feature: Native WebSockets (Updates + Method Calls)

Webhooks are great until you hit burst traffic. Every incoming update triggers a separate HTTP request, TLS/TCP overhead, and payload serialization—and every outgoing sendMessage or getChat requires another HTTP round-trip.

With LaraGram APIServer's WebSocket transport, your bot opens one persistent connection and handles everything asynchronously:

  • Incoming updates are pushed immediately to your bot with sub-millisecond latency.
  • Outgoing API method calls (like sendMessage, editMessageText, answerCallbackQuery) are sent right back over the exact same socket—eliminating HTTP headers and connection pooling bottlenecks.

Benchmarks: LaraGram APIServer vs. Official telegram-bot-api

We ran two sets of benchmarks:

  1. Direct Head-to-Head (getMe throughput & 24-hour systemd resource usage) against the official C++ telegram-bot-api binary.
  2. Transport Stress Tests comparing WebSocket vs. Webhook vs. Long Polling across 6 high-load scenarios.

1. Head-to-Head: LaraGram APIServer vs. Official C++ Bot API

Across 3 × 1,000 getMe runs and continuous 24-hour production monitoring via systemd (CPUUsageNSec and peak RSS memory):

Metric Official Bot API (C++) LaraGram APIServer (v0.6.1) Improvement
24-Hour Total CPU Time ~30 minutes < 30 seconds > 60× less CPU time
Throughput (Concurrency 1) 1,245.5 RPS 1,538.5 RPS +23.5% faster
Throughput (Concurrency 4) 881.5 RPS 951.8 RPS +8.0% faster
Server CPU / 1,000 req (c=1) 222.2 ms 187.0 ms -15.8% CPU
Server CPU / 1,000 req (c=4) 287.0 ms 220.6 ms -23.1% CPU
p99 Latency (c=1) 16.79 ms (spike) 1.597 ms 10.5× lower tail latency
Peak Systemd Memory (9 bots) — 116.3 MiB Rock-solid stability (0/6,000 errors)

Even in real-world 24/7 operation running 9 active bots simultaneously, LaraGram APIServer consumed under 30 seconds of total CPU time over 24 hours (compared to ~30 minutes on the official C++ binary—a 60× reduction), while RSS memory hovered around ~116–142 MB with zero dropped requests.


2. Transport Showdown: WebSocket vs. Webhook vs. Long Polling

Where LaraGram APIServer truly pulls away is its WebSocket layer. Here is how WebSocket stacks up against Webhook and Long Polling across massive update bursts and high-concurrency API calls:

Scenario A: High Volume (100,000 updates arriving at once)

Metric WebSocket Webhook Long Polling
Total Delivery Time 0.34 s 7.8 s 2.0 s
Throughput (updates/sec) 291,000 13,000 50,000
Server CPU per update 1.5 μs 81 μs 15 μs
Network bytes per update 231 580 207
Packet Count 578 200,404 2,037
Client CPU 409 ms 4,236 ms 914 ms
Peak Server Memory 140 MB 147 MB 136 MB

Takeaway: WebSocket delivers 100k updates in 340 milliseconds at 291,000 updates/sec—22.9× faster than Webhooks while using 54× less server CPU per update (1.5 μs vs 81 μs) and 346× fewer network packets.

Scenario B: Accumulated Queue Drain (100,000 queued updates)

Metric WebSocket Webhook Long Polling
Queue Drain Time 0.35 s 7.8 s 2.1 s
Throughput (updates/sec) 286,000 13,000 48,000
Server CPU per update 1.1 μs 78 μs 14 μs
Packet Count 629 200,412 2,026
Client CPU 413 ms 4,211 ms 924 ms

Takeaway: When your bot reconnects after downtime with 100k pending updates, WebSocket drains the entire backlog in 350 milliseconds at 286,000 updates/sec—over 22× faster than Webhooks.

Scenario C: Method Invocation (20,000 API requests, 50 concurrent)

Metric Over WebSocket HTTP (keep-alive)
Requests per second 28,675 RPS 8,162 RPS
Latency (p50 / p99) 1.7 / 2.5 ms 5.0 / 15.4 ms
Server CPU per request 9.9 μs 33 μs
Network bytes per request 94 B 436 B
Client CPU 764 ms 3,560 ms

Takeaway: Calling Bot API methods over WebSocket delivers 3.5× higher throughput (28.6k RPS vs. 8.1k RPS), cuts bandwidth per request by 78% (94 B vs 436 B), and keeps p99 latency flat at 2.5 ms.

Scenario D: Sustained Load & Idle Efficiency

  • Sustained Load (2,000 updates/sec for 15s): WebSocket maintained a 0.74 ms p50 / 1.2 ms p99 latency with a 6.0 ms max spike (compared to a 17.0 ms spike on Webhooks) while using just 12% of 1 CPU core (vs. 43% for Webhooks).
  • Single Updates (200 updates @ 20 ms intervals): 0.69 ms p50 / 1.7 ms p99 latency and only 121 ms total client CPU (vs. 208 ms on Webhook and 545 ms on Long Polling).
  • Idle (60s open connection, 0 updates): Virtually zero overhead (3.7 ms total CPU for WebSocket over 60 seconds).

Getting Started in 60 Seconds

You don't need a LaraGram project to use LaraGram APIServer—you can install it globally as a standalone server and use it with any Telegram bot library (Python, Node.js, Go, Rust, PHP, etc.):

# 1. Install globally via Composer
composer global require laraxgram/apiserver

# 2. Start the server (make sure Composer's global vendor/bin is in your PATH)
laragram-server --api-id=YOUR_API_ID --api-hash=YOUR_API_HASH --local
Enter fullscreen mode Exit fullscreen mode

Once running, simply point your bot's API base URL to your local server (e.g., http://127.0.0.1:8081), or connect directly via WebSocket to unlock full bidirectional streaming—no changes to your existing bot code required.


Check Out the Code & Docs

LaraGram APIServer is completely open-source under the MIT license:

If you're building high-throughput Telegram bots, userbots, or Mini Apps, give it a star on GitHub and let us know what benchmarks you hit!

Top comments (0)