Skip to content
Ctrip Flight App Replaces Polling with SSE: Full-Stack Streaming Implementation & 11.8% Faster Multi-Batch Loading
Mobile Development 15 min read

Ctrip Flight App Replaces Polling with SSE: Full-Stack Streaming Implementation & 11.8% Faster Multi-Batch Loading

Ctrip Flight team replaced polling with Server-Sent Events (SSE) across Web and mobile (Native + React Native), detailing implementation layers, optimizations like pre-connection, pre-request, caching, and Brotli compression, achieving 11.8% faster multi-batch loading and enabling heterogeneous batching for API granularity conflicts.

Ctrip Technology
Ctrip Technology
Ctrip Technology

SSE (Server-Sent Events) is a unidirectional HTTP-based streaming technology standardized in HTML5, allowing servers to push real-time updates to clients. Unlike WebSocket, SSE only supports server-to-client communication, making it lighter for scenarios where the client does not need to send frequent messages.

Background: Moving from Polling to SSE

The Ctrip Flight frontend team first adopted SSE on the Web side (detailed in a previous article "干货 | 携程机票前端Web流式通信SSE全链路应用实践"). The Web implementation used the fetch-event-source library on the client and required full-link SSE support across multiple Layer 7 proxies: Akamai/AWS edge nodes, SLB traffic ingress, "Wormhole" forwarding nodes, and Sidecar (Envoy/Nginx). Key server-side requirements included disabling response buffering on all Nginx hops via proxy_pass_header X-Accel-Buffering, enabling chunked transfer encoding, and turning on compression.

APP Implementation: Three-Layer Architecture

For the mobile APP (Native + React Native), the team built on the company's common framework which exposes SSE primitives. The architecture spans three layers:

Cloud layer : SSE endpoint returning text/event-stream.

Native layer : Platform-specific network libraries (iOS/Android) handling onOpen, onMessage, onClosed, onError callbacks.

RN business layer : A React Native plugin bridging Native events to JS, exposing the same four callbacks.

The RN plugin structure is illustrated in the article's diagrams (omitted here). Business code creates an SSE instance, subscribes to events, and processes batched data pushed by the server.

Optimization Techniques

1. Pre-connection (Host-level)

Before the actual request, the client establishes a TCP/TLS connection to the target host. Subsequent requests to the same host reuse this idle connection, reducing handshake latency. Example implementation:

ssePreConnect = (serviceCode: string): boolean => {</code><code>  const host = getHost(serviceCode);</code><code>  if (host && EventSource.preConnect) {</code><code>    EventSource.preConnect?.(host);</code><code>    return true;</code><code>  }</code><code>  .....;</code><code>};

2. Pre-request (Request-level)

In the business pre-flow, the full SSE request is issued early. The response stream is cached so that when the UI actually needs data, it can be served immediately from the in-flight request.

export const createSSEPreload = (options: TSSEServerParam) => {</code><code>  const { params, sseInstance } = createSSEInstance(options);</code><code>  const sseEventMananger = new SSEEventManager({params});</code><code>  sseInstance.preload?.();</code><code>  return () => {</code><code>    sseEventMananger.onUnsubscribe?.();</code><code>    sseInstance.removeAllEventListeners();</code><code>    sseInstance.close();</code><code>  };</code><code>};

3. Request Caching with Parent-Child Task Model

To serve multiple identical pre-requests, the framework uses a parent task (real network request) and child tasks (in-flight requests sharing the same cache key). When a child task attaches, it receives streamed data synchronously from the parent. A state enum tracks reuse status (e.g., CACHE_HIT, IN_FLIGHT, NEW_REQUEST). Flow diagrams in the article illustrate the lifecycle.

4. Brotli Compression Replacing Gzip

Switching to SSE changed the compression algorithm from ZSTD (used in the previous TCP-based protocol) to Gzip, increasing payload size and slightly hurting first-chunk latency. The team migrated to Brotli (Br):

Client metrics: Br compression ratio matches ZSTD; average payload size ~20% smaller than Gzip, ~30% smaller for extra-large payloads; SSE-Br improves end-to-end latency by ~20 ms over SSE-Gzip.

Server metrics: compression ratio comparable; no significant memory overhead; CPU overhead negligible; average compression time drops ~30% (small absolute value, mainly proves no regression).

Br is a standard HTTP encoding, so framework integration cost is low and works across all clients.

Performance Results

Compared with the legacy polling approach, SSE on APP delivered:

Reduced network interactions : fewer round-trips, fewer RN↔Native bridge calls, eliminated serial query wait times.

Simplified code : removed callback chains and sequencing logic for serial requests; pre-search logic now triggers once for all batches.

Lightweight, reusable across endpoints : easier to extend query mechanisms.

Measured improvement : multi-batch list loading total time improved by 11.8% on average . First-batch latency showed no significant difference. The gain comes from saving RN-Native communication (tens of ms), removing one request, and avoiding the "data ready but waiting for client poll" gap — especially impactful on high-frequency routes with unstable mobile networks.

Caveats observed in production:

SSE uses HTTP instead of raw TCP; SSL Pinning blocks some VPN users — requires fallback or pinning adjustment.

Overseas/long-haul routes need full-link SSE support; partial proxy support causes all batches to arrive simultaneously, requiring degradation handling.

Native auto-reconnect exists but is disabled to avoid reconnection storms under extreme load.

Heterogeneous Batching: Solving API Granularity vs. View Rendering

Frontend views often need data that cuts across multiple fine-grained, resource-oriented backend APIs. This forces complex client-side orchestration (request sequencing, rendering ordering, cross-API dependencies). The team proposes aggregating heterogeneous APIs into a single SSE stream, letting the server control batching and push order. Benefits:

Logic moves to backend, simplifying frontend.

Flexible per-element rendering control.

Multi-end reuse (Web, APP, future channels).

Pre-request and caching complexity drops significantly.

A comparison table (shown in the article) positions SSE against BFF aggregation and GraphQL: SSE wins on streaming delivery, zero client-side schema stitching, and minimal infrastructure change.

Future: AI Agent Streaming Foundation

The SSE infrastructure also prepares the platform for AI Agent scenarios where token-by-token streaming responses need to be rendered incrementally in list UIs. The same heterogeneous batching mechanism can interleave Agent output with traditional query results.

Conclusion

SSE's value is not raw speed over WebSocket, but achieving server push, full-link connection reuse, and native reconnection/error handling with minimal change — standard HTTP at the lowest integration cost. When the pattern is "server continuously emits data to client", SSE is the pragmatic choice.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mobile developmentperformance optimizationReact NativeServer-Sent EventsSSECtripBrotli CompressionStreaming Communication
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.