How Baidu’s iOS Long‑Connection SDK Powers Real‑Time Mobile Apps
This article examines the evolution of long‑connection technology for mobile apps, outlines Baidu’s unified long‑connection service architecture, details the iOS SDK’s design choices—including protocol selection, DNS optimization, token handling, heartbeat and reconnection mechanisms—and demonstrates its real‑time messaging use cases within the Baidu App.
1. Why Long Connections Matter on Mobile
Over the past decade, mobile applications have become the primary way users access the Internet, demanding higher efficiency and stability in client‑server communication. Maintaining a persistent TCP/UDP link—known as a long connection—eliminates the overhead of repeatedly establishing short connections, thereby improving latency, reliability, and overall user experience.
2. Long Connection Basics
A long connection allows multiple data packets to be exchanged over a single socket while keeping the link alive with periodic keep‑alive packets. Compared with short connections, it reduces connection‑setup cost and enables real‑time features such as instant messaging, push notifications, live streaming comments, and location updates. Popular apps like WeChat and QQ rely on this technique.
3. Baidu’s Unified Long‑Connection Service
Baidu previously let each business unit manage its own connection, leading to high maintenance costs and low reusability. The new service provides a high‑concurrency, low‑latency, high‑reachability component that can be shared across all Baidu apps. It consists of a client‑side SDK and a server‑side access layer (access control + connection gateway). The overall flow includes token acquisition, DNS resolution, socket creation, login authentication, and subsequent data exchange.
4. Building the iOS Long‑Connection SDK
The SDK construction faced three major challenges:
Protocol selection : TCP was chosen as the default for its reliability, while a small‑traffic experiment introduced QUIC (UDP‑based) to reduce handshake latency.
DNS resolution : Baidu replaced carrier‑provided LocalDNS with HTTPDNS to avoid hijacking and improve lookup speed.
Connection creation & maintenance : Designing token handling, socket management, heartbeat, and reconnection logic.
Two implementation schemes are provided:
Scheme 1 – a CocoaAsyncSocket‑based TCP implementation with TLS/SSL, used by ~90 % of iOS traffic.
Scheme 2 – a QUIC‑based experiment (10 % of traffic) using NWConnection, offering faster handshakes and multiplexing.
4.1 Connection Creation Process
The SDK follows four steps:
Token acquisition : Retrieves an access‑token (and protocol metadata) from a short‑connection endpoint, caching it locally.
DNS resolution : Uses HTTPDNS; results are cached for subsequent connections.
Socket establishment : Chooses TCP or QUIC based on the token metadata (90 % TCP, 10 % QUIC).
Login request : Sends the token to the server for authentication; a successful response marks the connection as ready for business traffic.
4.2 Connection Maintenance
Two mechanisms keep the link alive:
Heartbeat : After login, the server may specify a heartbeat interval; otherwise the SDK defaults to 60 seconds. Heartbeat packets are sent at the defined interval to detect broken links.
Reconnection : When the SDK detects a broken link (e.g., app backgrounded, network change), it triggers a serial‑queue‑based reconnection routine that avoids duplicate connection attempts.
5. Real‑World Use Cases in Baidu App
The long‑connection service underpins many Baidu App features, including real‑time health consultations, exam‑preparation chat, live‑stream comment streams, and push‑based configuration updates. In the IM SDK, long connections handle both upstream message sending and downstream push notifications.
5.1 Sending a Chat Message
When a user sends a message, the client creates a request object, fills in parameters, registers a callback, and dispatches it through the long‑connection SDK. The SDK queues the request, writes it to the socket, and matches the server’s response using a composite key (serviceId + methodId + timestamp).
5.2 Receiving New‑Message Notifications
The SDK registers persistent listeners for specific serviceId/methodId pairs. When the server pushes a notification, the read‑stream extracts the methodId, finds the matching request in the cache, and delivers the payload to the IM layer, enabling instant message display without additional polling.
6. Conclusions and Outlook
The Baidu long‑connection service comprises three core phases—connection establishment, maintenance, and data transmission. By addressing protocol selection, DNS security, token management, heartbeat, and reconnection, the SDK provides a robust foundation for real‑time mobile scenarios. With the rollout of 5G/6G networks, long‑connection technology is expected to expand into IoT, AR/VR, and other latency‑sensitive domains.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
