STOMP over WebSocket
subprotocolSTOMP (Simple Text Oriented Messaging Protocol) over WebSocket uses the subprotocol identifiers v10.stomp, v11.stomp, or v12.stomp. STOMP is a text-based messaging protocol similar to HTTP frames. WebSocket + STOMP is the standard integration for Spring WebSocket (Spring Boot) and popular with Java enterprise applications. SockJS is a fallback library that emulates WebSocket with long-polling when WebSocket is unavailable.
Details
STOMP over WebSocket is the standard approach for Spring Framework WebSocket messaging.
STOMP frame structure: COMMAND\n header1:value1\n header2:value2\n \n body^@ (null byte terminates body)
STOMP commands: CONNECT/CONNECTED: handshake SEND: client sends message to a destination (topic or queue) SUBSCRIBE: client subscribes to a destination UNSUBSCRIBE: cancel subscription MESSAGE: server pushes to subscribed client ACK/NACK: client acknowledges or rejects a message (for QoS) DISCONNECT: graceful close
Spring Integration: Spring's @MessageMapping and @SendTo annotations handle STOMP destinations. @Controller class handles /app/chat.sendMessage → sends to /topic/public. The SimpleBroker or RabbitMQ/ActiveMQ broker relay manages subscription routing.
SockJS fallback: SockJS clients try WebSocket first; fall back to HTTP streaming, XHR streaming, XHR polling if WebSocket is blocked. Server must enable SockJS endpoint alongside native WebSocket.
Handshake example
# STOMP over WebSocket
GET /ws HTTP/1.1
Upgrade: websocket
Sec-WebSocket-Protocol: v12.stomp,v11.stomp,v10.stomp
# Server selects version
Sec-WebSocket-Protocol: v12.stomp
# STOMP CONNECT frame (inside WebSocket text frame)
CONNECT
accept-version:1.2
heart-beat:10000,10000
Authorization:Bearer token123
^@
# Server CONNECTED frame
CONNECTED
version:1.2
heart-beat:10000,10000
^@
# Client SUBSCRIBE
SUBSCRIBE
id:sub-0
destination:/topic/messages
ack:client
^@
# Server MESSAGE
MESSAGE
destination:/topic/messages
subscription:sub-0
content-type:application/json
{"text":"Hello world"}^@