STOMP Mesajlarını Gönderme ve Alma
Konuya ve kullanıcıya özel hedefler dâhil olmak üzere STOMP kullanarak mesaj üreticileri ve tüketicileri hayata geçirin.
STOMP Mesajlarını Gönderme ve Alma, CoddyKit'te ücretsiz bir WebSockets & Real-Time Systems with Spring dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, WebSockets & Real-Time Systems with Spring öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. WebSockets & Real-Time Systems with Spring kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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!
Sıkça Sorulan Sorular
“STOMP Mesajlarını Gönderme ve Alma” dersi ücretsiz mi?
Evet — “STOMP Mesajlarını Gönderme ve Alma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve WebSockets & Real-Time Systems with Spring kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. WebSockets & Real-Time Systems with Spring kursu toplamda 4 dersten oluşur.
“STOMP Mesajlarını Gönderme ve Alma” dersinde ne öğreneceğim?
Konuya ve kullanıcıya özel hedefler dâhil olmak üzere STOMP kullanarak mesaj üreticileri ve tüketicileri hayata geçirin. WebSockets & Real-Time Systems with Spring ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
WebSockets & Real-Time Systems with Spring öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te WebSockets & Real-Time Systems with Spring, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“STOMP Mesajlarını Gönderme ve Alma” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu WebSockets & Real-Time Systems with Spring dersinde kod yazıp çalıştırabilir miyim?
Evet. Her WebSockets & Real-Time Systems with Spring dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- STOMP Protokolüne Giriş
- STOMP'u Spring ile Yapılandırma
- STOMP Mesajlarını Gönderme ve Alma
- Spring Security ile STOMP Uç Noktalarını Güvenceye Alma