API Patterns and Practices: WebSockets
Designing WebSocket APIs well, with runnable examples in Quarkus WebSockets Next and Python FastAPI/websockets against one AsyncAPI 3 contract, fanned out through a live handling-event feed.
What this site is
A chapter-based, hands-on path to designing WebSocket APIs well, built around a
single AsyncAPI 3 contract for a shipping
and cargo domain. The socket here pushes a live handling-event feed
(the stream of container moves, customs holds, and delivery confirmations a shipment
generates as it moves) to any client watching a booking, instead of making that
client poll the sibling
REST
booking service for a status that hasn't changed. Every hands-on chapter ships the
same example in both Quarkus WebSockets Next and Python
(FastAPI/Starlette plus the native websockets library), generated
against that one contract. You work through message design and versioning,
backpressure and flow control, heartbeats and reconnect (both stateless and
Kafka-backed), security, fan-out contrasted on Kafka and on Redis, binary Protobuf
framing, testing and debugging a protocol you can't just `curl`, and the anti-patterns
that show up once a socket is in production. It assumes you can read Java and Python;
it is not a tutorial in either language. The code tabs are built to admit Go and C++
later. All container images are UBI-based, and the stack runs on Docker Compose,
minikube, and OpenShift Local.
Prerequisites
Get the toolchain and the Docker Compose lab running locally: Quarkus WebSockets Next, Python, LGTM observability, and the shipping/cargo domain this site's sockets share with the REST booking service.
Foundations
Why push beats polling, how to choose among WebSockets, SSE, long-polling, and webhooks, how the protocol itself works, and the shared domain every later chapter builds on.
Contract-first for Sockets
Give a socket a contract with AsyncAPI 3, shape its messages, and version them without breaking a connected client.
Building It
Build the same socket in Quarkus WebSockets Next and in Python's native websockets, build clients that reconnect and respect backpressure, and verify both stacks against one contract.
Production Concerns
Backpressure and flow control, error handling, liveness and heartbeats, stateless and Kafka-backed reconnect, security, and fan-out at scale: the concerns that show up once a socket is live.
Performance & Optimization
Binary Protobuf framing over WebSockets, and the throughput work that follows once a socket carries real load.
Testing, Debugging & Observability
Test a protocol you can't curl, debug a connection that drops without a client-side error, trace a message end to end, and recognize the anti-patterns that make all of this harder.
Runtime Progression
Run the stack on Docker Compose, then on Kubernetes via minikube, then as a separate OpenShift Local (CRC) appendix.
The Bigger Picture
Know when a socket is the wrong tool, and bring every pattern in this site together in one final project.