Understanding RTMP Handshake, Chunking, and Nginx‑RTMP Live Streaming Implementation
This article explains the RTMP protocol’s handshake process, chunking and message formats, details command flows such as connect, createStream, publish and play, and describes how the Nginx‑RTMP module implements live streaming, acceleration, authentication, recording and HLS slicing.
RTMP (Real Time Messaging Protocol) is an Adobe‑origin application‑layer protocol that multiplexes audio, video and data streams over TCP. Before data transmission, both client and server perform a handshake (simple or complex) to negotiate version, encryption and user validation.
The handshake involves exchanging C0+C1, S0+S1, S2 and C2 packets; C0/S0 are single‑byte version fields, while C1/S1 carry timestamps, version numbers and random data. The server generates S2 by encrypting C1, and the client replies with C2 encrypted from S1. After the handshake, RTMP operates in two layers: the message layer (messages) and the chunk layer (chunks).
Chunks are the smallest transmission units. The chunk header (basic header, message header, extended timestamp) determines how much of the original message header is repeated. Chunk size defaults to 128 bytes but can be increased (e.g., 4096) to improve throughput. The protocol defines four chunk types (fmt 0‑3) that control header length and field omission.
RTMP messages are categorized as command, data, audio/video, or user‑control events. Commands such as connect , createStream , publish , play and deleteStream use AMF0/AMF3 encoding and include transaction IDs for correlating responses.
The Nginx‑RTMP module (nginx‑rtmp‑module) extends the high‑performance Nginx server to provide a live‑streaming server. Its configuration resides in an rtmp {} block with server {} and application {} sections, allowing settings for listening ports, live mode, authentication callbacks, relay, recording, and HLS slicing.
Authentication is performed via HTTP callbacks (on_publish, on_play) where a 2XX response accepts the session, while a 3XX response redirects the stream. The module also supports static relay (ngx_rtmp_relay_module) and global pull for stream acceleration, as well as the recording module (ngx_rtmp_record_module) for persisting streams.
HLS support is provided by ngx_rtmp_hls_module, which generates .m3u8 playlists and .ts/.mp4 segments in real time, enabling HTTP‑based playback on client devices.
Xueersi Online School Tech Team
The Xueersi Online School Tech Team, dedicated to innovating and promoting internet education technology.
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.