Video in progress

What is STOMP, and why is it used on top of WebSocket?

STOMP is a simple text-based messaging protocol with commands like CONNECT, SUBSCRIBE, SEND and MESSAGE, plus headers. WebSocket only "delivers bytes"; STOMP adds destinations, subscriptions and routing on top.

📖 Answer

With raw WebSocket you invent things like "which room is this message for" and "who is subscribed to what" yourself. STOMP gives you that convention ready-made.

A STOMP frame looks like this:

SEND
destination:/app/chat.send
content-type:application/json

{"room":"java","text":"salom"}^@

(^@ is the null byte, the end of the frame.)

With STOMP in Spring, the code looks like an ordinary controller:

@MessageMapping("/chat.send")
@SendTo("/topic/chat")
public ChatMessage send(ChatMessage msg) {
    return msg;
}

On top of that, STOMP lets you connect to ready-made brokers (RabbitMQ, ActiveMQ), authorize at the destination level with Spring Security, and use client libraries such as @stomp/stompjs.

The cost is a bit of extra text on every message and one more layer of abstraction. For very high-frequency binary streams (games, audio), STOMP is usually not used.