Skip to content

Lesson 2 of 9 · 20m

Realtime Protocol Design: Messages, Versions, and Payload Discipline

Designing message envelopes, versioning wire contracts, and keeping payloads small enough to survive real traffic.

Realtime systems fail socially before they fail technically. One team adds a field, another deploys a client that expects it, and now half the fleet is throwing parse errors. The fix is not more discipline meetings; it is a boring, explicit wire protocol.

Every message gets an envelope

{
  "v": 1,
  "type": "chat.message.created",
  "id": "01J9Z3KQZ2M8D7R4",
  "ts": 1732924800123,
  "room": "room:42",
  "data": {
    "body": "deploy finished",
    "author": "u_712"
  }
}

The envelope carries routing and lifecycle metadata. type tells clients how to interpret data; id enables deduplication; ts helps diagnostics; v gives you a migration path. Payload-specific fields stay inside data so generic infrastructure can inspect messages without understanding every feature.

Do not let the server emit whatever object the database returned. Database rows are internal contracts, wire messages are public contracts.

Commands and events are different directions

Clients send commands: subscribe, typing.start, cursor.move. Servers emit events: message.created, presence.changed, snapshot.ready. Make that direction obvious in naming. A client should never be able to forge presence.changed for another user merely because the schema allows the type.

A healthy protocol usually has three response classes:

command accepted       -> ack with command id
command rejected       -> error with machine-readable code
state changed later    -> event broadcast to subscribers

Acks are not optional. Without them a client cannot distinguish "server never received it" from "server received it and the broadcast was lost," and every reconnect strategy becomes guesswork.

Version the contract, not every field

Additive changes are safe: a new optional field, a new message type, a new enum value clients can ignore. Breaking changes are removing fields, changing units, reusing a type with different meaning, or tightening validation on data old clients still send.

Use v for envelope shape and negotiated subprotocols for major protocol families. In practice, you will support two versions during migration, not ten. Publish a deprecation date and measure traffic by version before removal.

Small messages beat clever compression

A typing indicator does not need the user's profile object. A cursor update does not need the document title. Send IDs and deltas; let clients hydrate from local caches or a snapshot endpoint. Compression helps repeated JSON, but it cannot fix a design that ships the world every 80 milliseconds.

bad:  { type:"cursor", user:{id,name,avatar,email,roles...}, doc:{...} }
good: { type:"cursor.move", user:"u_712", x:184, y:72, seq:991 }

Field notes

  • Treat message schemas like public APIs even if the only consumer is your own frontend.
  • Include a server-generated event ID on anything a client might need to deduplicate or resume.
  • Validate inbound messages at the connection boundary. A malformed frame should kill one message, not corrupt shared state.
  • Put size limits on the socket and per message type. The largest legitimate payload determines the limit, not the average payload.

Was this lesson helpful?

Sign in to rate