การส่งและรับข้อความ STOMP
นำผู้ผลิตและผู้บริโภคข้อความด้วย STOMP ไปใช้ ซึ่งรวมถึงปลายทางตามหัวข้อและปลายทางเฉพาะผู้ใช้
การส่งและรับข้อความ STOMP เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to STOMP Messaging
In this lesson, we'll learn how to send messages from a client to our Spring server and how the server can then send messages back, either to a general topic or a specific user.
STOMP provides a clear, structured way to route these real-time messages, making our applications more predictable and easier to manage.
Producers and Consumers
Think of messaging in terms of producers and consumers:
- Producers send messages (e.g., a chat client sending a message).
- Consumers receive messages (e.g., another chat client displaying the message).
In a real-time app, clients often act as both!
Sending to Public Topics
A topic is a public destination. When a message is sent to a topic, all clients currently subscribed to that topic will receive the message.
It's like a broadcast channel or a public chat room where everyone hears what's said.
Common topic paths start with /topic/, e.g., /topic/public-chat or /topic/news-feed.
Server: Handling Topic Messages
On the Spring server, we use @MessageMapping to handle incoming messages from clients to a specific destination. We can then use SimpMessagingTemplate to send messages back out to a topic.
This snippet shows a server method receiving a message and then broadcasting it to a topic.
@Controller
public class ChatController {
@Autowired
private SimpMessagingTemplate messagingTemplate;
@MessageMapping("/chat.sendMessage")
public void sendMessage(@Payload String chatMessage) {
// Client sends to /app/chat.sendMessage
// Server broadcasts to /topic/public-chat
messagingTemplate.convertAndSend(
"/topic/public-chat", "New message: " + chatMessage);
}
}Client: Subscribing to Topics
On the client-side (e.g., JavaScript), you subscribe to a topic to receive messages. When the server sends a message to /topic/public-chat, all subscribed clients get it.
A client might subscribe like this:
stompClient.subscribe('/topic/public-chat', function(message) {
// Handle received message
});User-Specific Messaging
Sometimes you need to send a message directly to a single, specific user, not a public topic. This is called user-specific messaging.
It's perfect for:
- Private chat messages
- Personal notifications
- Direct alerts
STOMP and Spring handle the routing automatically for you.
Server: Sending to a User
Spring's SimpMessagingTemplate has a special method, convertAndSendToUser, to target messages to a specific user. You provide the username and the specific destination within that user's queue.
The framework translates /user/{username}/queue/messages into a unique queue for that user.
@Controller
public class UserController {
@Autowired
private SimpMessagingTemplate messagingTemplate;
@MessageMapping("/private.send")
public void sendPrivateMessage(
@Payload String privateMessage, Principal principal) {
// principal.getName() gets the current user's username
String recipient = "someOtherUser"; // This would come from payload
messagingTemplate.convertAndSendToUser(
recipient, "/queue/private-messages",
"From " + principal.getName() + ": " + privateMessage);
}
}Client: Subscribing to User Queue
A client subscribes to their own user-specific queue to receive private messages. The path usually looks like /user/queue/private-messages.
The key here is that the /user prefix is special. The STOMP broker automatically routes messages sent to a particular user to their unique session-specific queue.
Message Payloads
Messages sent over STOMP usually carry data in their payload (the message body). The most common format for this data is JSON.
Spring's STOMP support automatically converts Java objects to JSON (and vice-versa) when you use @Payload and convertAndSend methods, making it super easy to work with structured data.
Quick Check: STOMP Destinations
You want to send a personal notification to only one specific user. Which type of STOMP destination should you use?
Recap: Sending & Receiving
We've explored how to send and receive STOMP messages in Spring applications:
- Topics (e.g.,
/topic/public-chat) are for broadcasting messages to all subscribed clients. - User-specific destinations (e.g.,
/user/queue/private-messages) are for sending private messages to a single user. - Spring's
SimpMessagingTemplateand@MessageMappingannotations simplify both sending and receiving.
You can now build interactive features like chat rooms and private notifications!
เรียนรู้ WebSockets & Real-Time Systems with Spring ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การส่งและรับข้อความ STOMP” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การส่งและรับข้อความ STOMP” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การส่งและรับข้อความ STOMP”
นำผู้ผลิตและผู้บริโภคข้อความด้วย STOMP ไปใช้ ซึ่งรวมถึงปลายทางตามหัวข้อและปลายทางเฉพาะผู้ใช้ คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 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