Websocket
A WebSocket is a two-way, persistent communication channel between a client and a server.
- Like a phone call.
- Once the call is connected, both you (the client) and the person on the other end (the server) can talk and listen at any time. The line stays open until one of you hangs up.
- How it works
- The client requests to “upgrade” a standard HTTP connection to a WebSocket connection. If the server agrees, this single connection stays open, allowing for full-duplex (two-way) data flow.
- After the “upgrade”, messages inside the WebSocket pipe (like
SENDorSUBSCRIBE) do not go through the HTTP security chain
- After the “upgrade”, messages inside the WebSocket pipe (like
- With this API, you can send messages to a server and receive responses without having to poll the server for a reply
- The client requests to “upgrade” a standard HTTP connection to a WebSocket connection. If the server agrees, this single connection stays open, allowing for full-duplex (two-way) data flow.
- Data Flow: Bidirectional (Client ↔ Server).
- Common Use Cases:
- Chat Apps: You can send a message, and you can receive a message instantly.
- Online Games: Your browser needs to constantly send your actions (e.g., “I moved left”) and receive the game’s state (e.g., “An enemy appeared”).
- Live Editing: Collaborative documents (like Google Docs) where you can see others’ cursors and typing in real-time.
- Using Websockets in SpringBoot
STOMP
- STOMP stands for Simple Text Oriented Messaging Protocol
- a text-based protocol for exchanging messages through a message broker
- Main job is to define a common “language” for clients and servers to exchange messages (using message brokers)
- If HTTP is request-response based, STOMP follows a Publish-Subscribe (pub-sub) model.
- Instead of HTTP request methods
GETorPOST, it uses commands like:CONNECT(to connect to a broker)- The very first message a STOMP client sends after the WebSocket connection is established
SUBSCRIBE(to listen to a specific “topic” or queue, e.g.,SUBSCRIBE /topic/chat-room-1)SEND(to publish a message to a topic, e.g.,SEND /app/chat)DISCONNECT
- A protocol that runs on top of websockets
- WebSocket : the transport layer
- The “pipe” that provides a fast, persistent, two-way connection between a client (like a browser) and a server. By itself, WebSocket doesn’t know what you’re sending. It’s just a channel for raw text or binary data.
- STOMP: the application-level protocol
- The “language” you speak through the WebSocket pipe. It adds the necessary structure for building real applications.
- WebSocket : the transport layer
Diagram

- Message broker
- an intermediary that facilitates communication between multiple clients, often using a publish/subscribe (pub/sub) model, by managing and routing messages between them
- Publisher (송신자)
- Sends messages to a specific topic
- No need to know who is subscribing or if they received it well
- Subscribers (수신자)
- Receives messages
- No need to know who sent the msg or how they are sent, just need to subscribe
Data transmission unit
- WebSocket transmits data in units called frames
- Each frame consists of metadata and the actual data (payload).
| Field | Size | Description |
|---|---|---|
| FIN | 1 bit | Whether this is the final frame of the message |
| Opcode | 4 bits | The type of frame (Text, Binary, Ping, etc.) - Text (Opcode 0x1): text message- Binary (Opcode 0x2): binary data- Close (Opcode 0x8): Close connection- Ping (Opcode 0x9): Check connection status request- Pong (Opcode 0xA): Ping response |
| Mask | 1 bit | Whether it is masked (Required from Client → Server) |
| Payload Length | 7~64 bits | Length of the data being sent |
| Payload Data | Variable | Actual data |
How it works (detail)

Upgrade request & 101 response
HTTP Handshake (upgrade request)
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13101 Switching protocols (response)
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=- From this point on, the connection is maintained using the WebSocket protocol, not the HTTP protocol.
Websocket Frame
- Sending each other data
Secure Connection (WSS)

- In environments requiring security:
- Use the WSS (WebSocket Secure) protocol
- This operates on top of the TLS (SSL) layer, just like HTTPS (ensuring data encryption and integrity)
- Actually u should just use this in your applications by default (it’s important)
| Classification | HTTP | HTTPS (WSS) |
|---|---|---|
| Protocol | ws:// | wss:// |
| Security Level | Low | High |
| Port Used | 80 | 443 |