المراسلة الموجهة إلى مستخدمين محددين
أرسل رسائل خاصة مخصصة لكل مستخدم عبر STOMP باستخدام وجهات المستخدم و@SendToUser وSimpMessagingTemplate.convertAndSendToUser.
المراسلة الموجهة إلى مستخدمين محددين درس مجاني في WebSockets & Real-Time Systems with Spring على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في WebSockets & Real-Time Systems with Spring، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة WebSockets & Real-Time Systems with Spring 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Beyond Broadcasts
So far messages went to a /topic that every subscriber receives. Real apps also need private messages: a chat whisper, a personal notification, an order update for one customer.
User Destinations
Spring supports the /user destination prefix. When a client subscribes to /user/queue/notifications, Spring rewrites it to a unique, per-session queue so only that user gets the message.
Where the Principal Comes From
Targeted messaging needs to know who a session is. The Principal attached during the handshake (from Spring Security or a custom handshake handler) provides the username key.
Sending with @SendToUser
In a controller method you annotate with @SendToUser. The return value is delivered only to the user who sent the triggering message.
@MessageMapping("/private")
@SendToUser("/queue/reply")
public Note handle(Note in, Principal principal) {
return new Note("Hi " + principal.getName());
}The Client Subscription
The client subscribes to the /user-prefixed destination. Spring resolves it to that client's private queue automatically.
stompClient.subscribe('/user/queue/reply', (msg) => {
console.log('Private:', JSON.parse(msg.body));
});Pushing from Anywhere with the Template
Outside a controller (e.g. from a service or scheduled job) use SimpMessagingTemplate.convertAndSendToUser to reach a specific user by name.
messagingTemplate.convertAndSendToUser(
"alice", "/queue/notifications", new Alert("Payment received"));How Resolution Works
The UserDestinationMessageHandler maps /user/{username}/queue/x to the actual session-specific destination. You address users by name; Spring finds their active sessions.
Multiple Sessions per User
One user may be connected from a phone and a laptop. convertAndSendToUser delivers to all of that user's active sessions, so notifications appear everywhere they are logged in.
Targeting a Single Session
To reach just one device, pass headers selecting a specific simpSessionId rather than broadcasting to every session of the user.
SimpMessageHeaderAccessor h = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
h.setSessionId(sessionId);
h.setLeaveMutable(true);
messagingTemplate.convertAndSendToUser(user, "/queue/reply", body, h.getMessageHeaders());User Destinations Across a Cluster
With a single server the simple broker resolves user destinations in-memory. Across multiple nodes you need an external broker (RabbitMQ) plus a UserRegistry broadcast so a node can route to a user connected elsewhere.
Security Note
Never let a client choose another user's destination. Always derive the target from the authenticated Principal, not from client-supplied data, to prevent message hijacking.
Quick Check
Test your knowledge of per-user messaging.
Recap
You delivered private messages:
- The
/userprefix maps to per-session queues @SendToUserreplies to the originating userconvertAndSendToUserpushes to a named user from anywhere- Multiple sessions per user are reached together; target one with session headers
- Always derive the target from the authenticated
Principal
تعلم WebSockets & Real-Time Systems with Spring مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «المراسلة الموجهة إلى مستخدمين محددين» مجاني؟
نعم — نص درس «المراسلة الموجهة إلى مستخدمين محددين» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Real-Time Systems with Spring، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Real-Time Systems with Spring 4 دروس في المجموع.
ماذا ستتعلم في «المراسلة الموجهة إلى مستخدمين محددين»؟
أرسل رسائل خاصة مخصصة لكل مستخدم عبر STOMP باستخدام وجهات المستخدم و@SendToUser وSimpMessagingTemplate.convertAndSendToUser. تتمرن على WebSockets & Real-Time Systems with Spring مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ WebSockets & Real-Time Systems with Spring؟
لا تُشترط خبرة سابقة. WebSockets & Real-Time Systems with Spring على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «المراسلة الموجهة إلى مستخدمين محددين»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس WebSockets & Real-Time Systems with Spring هذا؟
نعم. كل درس في WebSockets & Real-Time Systems with Spring يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- معترضات WebSocket
- تخصيص محوّلات الرسائل
- إدارة جلسات المستخدمين
- المراسلة الموجهة إلى مستخدمين محددين