Video in progress
How do you evolve and version a WebSocket-based API when it has to work with web and old mobile app versions?
Mobile clients can't be force-updated, so message formats only grow in a backward-compatible way, the protocol version is negotiated in the handshake, and there is an explicit shutdown plan for old versions.
📖 Answer
In REST you make /v2/orders and you're done. In WebSocket, dozens of message types pass through one connection, and a year-old app version in the App Store is still running.
The rules I apply:
Version in the handshake. Through the subprotocol header: Sec-WebSocket-Protocol: myapp.v3, myapp.v2. The server picks the one it supports and returns it in the response. In Spring, with DefaultHandshakeHandler.setSupportedProtocols(...). If you use STOMP, your own app-version value in the CONNECT header.
The message schema changes only by adding. A new field is optional. Removing a field or changing its meaning is a new message type. Clients must ignore unknown fields and unknown message types. If this rule isn't there from the first version, you can't add it later.
Server-driven capabilities. On connect the server sends the client a list of "capabilities I support", and the client sends its own. A new feature is turned on only if both sides know it. This is better than an if-else tree on version numbers.
Schema registry. JSON Schema or Protobuf, with a backward-compatibility check in CI. Protobuf's field numbers make evolution natural, but debugging in the browser is harder, because DevTools shows bytes.
Retiring an old version. Metrics should include the share of connections by version. When a version's share drops below 1%, the server returns a special close code for that version (for example 4426) and the app shows an "update" screen. This mechanism also has to be in the first version.
This is where teams fight most: the backend team changes a format and the mobile team doesn't know. Keeping the contract in the same repo as the code, plus contract tests, reduces this a lot.
Sign up to mark studied questions and save your progress.