Understanding the Atmosphere Protocol - Atmosphere/atmosphere GitHub Wiki

Understanding the Atmosphere Protocol

Advanced topic. The protocol is handled transparently by @ManagedService and atmosphere.js. This page documents the wire format for framework integrators or custom client implementations.

The Atmosphere Protocol is a lightweight handshake and message framing layer that works on top of any transport (WebSocket, SSE, long-polling, streaming).

Handshake

When enableProtocol is true (default), the first message from the server contains metadata:

uuid|heartbeatInterval|heartbeatPaddingChar

Example:

a1b2c3d4-e5f6-7890-abcd-ef1234567890|30|X

The client stores the UUID and uses it for reconnection.

Message Framing (TrackMessageSize)

When trackMessageLength is true, messages are prefixed with their length:

<length>|<message>

Example:

42|{"author":"Alice","message":"Hello!","time":1708200000000}

This ensures complete message delivery over streaming and long-polling transports where message boundaries may not be preserved.

Heartbeat

The server sends a single character (the heartbeatPaddingChar, default X) at regular intervals to keep connections alive across proxies and load balancers.

The client can also send heartbeats to the server, handled by the @Heartbeat annotation.

Transport Negotiation

The client connects with its preferred transport. If it fails:

  1. Client tries transport (e.g., websocket)
  2. If connection fails โ†’ tries fallbackTransport (e.g., long-polling)
  3. Notifies via transportFailure handler
  4. On disconnect โ†’ auto-reconnects with exponential backoff

Connection States

disconnected โ†’ connecting โ†’ connected โ†’ reconnecting โ†’ connected
                                     โ†˜ closed
                                     โ†˜ error

Client-Server Message Flow

Client                          Server
  โ”‚                               โ”‚
  โ”œโ”€โ”€โ”€โ”€ GET /chat (upgrade) โ”€โ”€โ”€โ”€โ”€โ†’โ”‚  WebSocket handshake
  โ”‚                               โ”‚
  โ”‚โ†โ”€โ”€ uuid|30|X โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚  Protocol handshake
  โ”‚                               โ”‚
  โ”‚โ†โ”€โ”€ X โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚  Heartbeat
  โ”‚                               โ”‚
  โ”œโ”€โ”€โ”€โ”€ {"author":"A","msg":"Hi"}โ†’โ”‚  Client message
  โ”‚                               โ”‚
  โ”‚โ†โ”€โ”€ 35|{"author":"A","msg":"Hi"}โ”‚  Broadcast (with length prefix)
  โ”‚                               โ”‚
โš ๏ธ **GitHub.com Fallback** โš ๏ธ