STOMP-Nachrichten senden und empfangen
Implementieren Sie mit STOMP Nachrichtenproduzenten und -konsumenten, einschließlich themenbezogener und benutzerspezifischer Ziele.
STOMP-Nachrichten senden und empfangen ist eine kostenlose WebSockets & Real-Time Systems with Spring-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des WebSockets & Real-Time Systems with Spring-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der WebSockets & Real-Time Systems with Spring-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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!
Häufig gestellte Fragen
Ist die Lektion „STOMP-Nachrichten senden und empfangen“ kostenlos?
Ja — der vollständige Text von „STOMP-Nachrichten senden und empfangen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des WebSockets & Real-Time Systems with Spring-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der WebSockets & Real-Time Systems with Spring-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „STOMP-Nachrichten senden und empfangen“?
Implementieren Sie mit STOMP Nachrichtenproduzenten und -konsumenten, einschließlich themenbezogener und benutzerspezifischer Ziele. Du übst WebSockets & Real-Time Systems with Spring mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um WebSockets & Real-Time Systems with Spring zu starten?
Keine Vorkenntnisse erforderlich. WebSockets & Real-Time Systems with Spring auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „STOMP-Nachrichten senden und empfangen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser WebSockets & Real-Time Systems with Spring-Lektion Code schreiben und ausführen?
Ja. Jede WebSockets & Real-Time Systems with Spring-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in das STOMP-Protokoll
- STOMP mit Spring konfigurieren
- STOMP-Nachrichten senden und empfangen
- STOMP-Endpunkte mit Spring Security absichern