Video in progress
The client must confirm that every event sent to it was delivered. How do you build an ack mechanism on top of STOMP?
STOMP has an ack:client-individual mode and ACK/NACK frames, but Spring's simple broker doesn't support them. So in practice you build your own ack at the application level: an ID on every message, the client confirms, and unconfirmed ones are resent.
📖 Answer
The protocol has the capability:
SUBSCRIBE
id:sub-1
destination:/queue/orders
ack:client-individual
The broker sends the message and keeps it as "undelivered" until the client returns ACK id:.... Through the RabbitMQ relay this works. But Spring's simple broker ignores ACK and NACK: it only does "send and forget".
Even with the relay not everything is smooth. If the connection drops, the unacknowledged message returns to the broker queue. If there's a separate queue for each session (which is how /user/... works), a reconnected client arrives with a new session and the message in the old queue is left hanging.
So I often do it at the application level:
- The event is stored in an outbox table or a Redis Stream with the status
PENDING. - The server sends it to the client with an
id. - After handling it, the client sends the
idto/app/ack, and the server marks itACKED. - A scheduler resends those that are
PENDINGand older than 30 s, if the user is online. - The client drops duplicates by
id.
Sending acks in batches rather than one by one cuts the load a lot: "I received everything up to this seq" is a cumulative ack. In an ordered stream that's enough.
You don't always need this kind of complexity. For notifications, "at most once" plus fetching the list over REST on reconnect is often cheaper and more reliable.
Sign up to mark studied questions and save your progress.