แนะนำโพรโทคอล STOMP
ทำความเข้าใจประโยชน์ของ STOMP เมื่อเทียบกับ WebSockets โดยตรง สำหรับการกำหนดเส้นทางและการสมัครรับข้อความอย่างเป็นโครงสร้าง
แนะนำโพรโทคอล STOMP เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Meet STOMP: Structured Messaging
Welcome to STOMP! When building real-time apps with WebSockets, you often need more than just raw data transfer. You need structure, routing, and a way to subscribe to specific messages.
STOMP (Simple Text Oriented Messaging Protocol) provides this structure. Think of it as a higher-level protocol that sits on top of WebSockets.
Raw WebSockets: Limitations
With raw WebSockets, clients and servers send arbitrary bytes or strings. If you want to send a "chat message" or a "user joined" event, you have to define your own rules:
- How do I know what kind of message it is?
- Who should receive this message?
- What if I want to send data in a specific format like JSON?
Without a protocol, you'd implement all this manually.
STOMP: A Common Language
STOMP solves these problems by defining a standard way to send and receive messages. It's like agreeing on a common language for your real-time communication.
- It's text-based, making it easy to debug.
- It uses a simple frame format for all communication.
- It supports common messaging patterns like publish-subscribe.
Understanding STOMP Frames
All communication in STOMP happens via "frames." A STOMP frame consists of three main parts:
- COMMAND: What action is being performed (e.g., SEND, SUBSCRIBE, CONNECT).
- HEADERS: Key-value pairs providing additional info (e.g., destination, content-type).
- BODY: The actual message payload.
These parts are separated by newlines.
First Frame: CONNECT
When a client wants to establish a STOMP connection over a WebSocket, it sends a CONNECT frame. This tells the server it wants to speak STOMP. Note the empty line before the null byte (^@) which terminates the frame.
Here's what a simple CONNECT frame looks like:
CONNECT
accept-version:1.2
host:localhost
Server's Reply: CONNECTED
If the server accepts the connection, it responds with a CONNECTED frame. This confirms the STOMP session is ready. It also includes headers like the server's version and session ID. Remember, frames end with a null byte (^@).
CONNECTED
version:1.2
session:session-1234
server:Spring
Sending Messages: The SEND Frame
To send a message, clients use the SEND command. This frame requires a destination header, which tells the STOMP broker where to route the message. The message body contains the actual data, often JSON. Don't forget the null byte (^@) terminator!
SEND
destination:/app/chat
content-type:application/json
{"user":"Alice","message":"Hello, everyone!"}
Receiving Messages: SUBSCRIBE
Clients receive messages by using the SUBSCRIBE command. They specify a destination to listen to and an id for their subscription. The server then sends MESSAGE frames to this client when new data arrives for that destination. Frames are always null-byte (^@) terminated.
SUBSCRIBE
id:sub-0
destination:/topic/public
STOMP Destinations: Routing
STOMP uses destinations to route messages. These are like addresses. The most common types are:
- Topics (
/topic/...): For broadcast messages, where all subscribers receive a copy. Great for chat rooms. - Queues (
/queue/...): For point-to-point messages, typically handled by one consumer. Useful for private messages or task queues.
This built-in routing is a major advantage over raw WebSockets.
Why Use STOMP?
STOMP provides significant benefits for real-time applications:
- Structured Messaging: Standardized frame format.
- Message Routing: Built-in support for topics and queues.
- Subscription Model: Easy for clients to listen to specific events.
- Simpler Client-Side: Many client libraries exist that handle STOMP frames, simplifying development.
It adds a robust layer for complex messaging patterns.
Check Your Understanding
Which of the following is a primary benefit of using STOMP over raw WebSockets?
Lesson Recap: Introducing STOMP
In this lesson, you learned about the STOMP protocol and why it's essential for structured real-time communication over WebSockets.
- Raw WebSockets lack built-in message structure and routing.
- STOMP provides a simple, text-based frame format (COMMAND, HEADERS, BODY).
- Key STOMP commands include
CONNECT,CONNECTED,SEND, andSUBSCRIBE. - It enables powerful features like topic-based broadcasting and queue-based point-to-point messaging.
Next, we'll see how to integrate STOMP with Spring Boot!
คำถามที่พบบ่อย
บทเรียน “แนะนำโพรโทคอล STOMP” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แนะนำโพรโทคอล STOMP” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แนะนำโพรโทคอล STOMP”
ทำความเข้าใจประโยชน์ของ STOMP เมื่อเทียบกับ WebSockets โดยตรง สำหรับการกำหนดเส้นทางและการสมัครรับข้อความอย่างเป็นโครงสร้าง คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “แนะนำโพรโทคอล STOMP” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม
ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แนะนำโพรโทคอล STOMP
- การตั้งค่า STOMP ร่วมกับ Spring
- การส่งและรับข้อความ STOMP
- การรักษาความปลอดภัยปลายทาง STOMP ด้วย Spring Security