WebSockets & Real-Time Systems with Spring · บทเรียน

การส่งข้อความเฉพาะเจาะจงถึงผู้ใช้

ส่งข้อความส่วนตัวถึงผู้ใช้แต่ละรายผ่าน STOMP โดยใช้จุดหมายปลายทางผู้ใช้ @SendToUser และ SimpMessagingTemplate.convertAndSendToUser

บทเรียน 4 จาก 413 ขั้นตอน

การส่งข้อความเฉพาะเจาะจงถึงผู้ใช้ เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 /user prefix maps to per-session queues
  • @SendToUser replies to the originating user
  • convertAndSendToUser pushes 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 ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การส่งข้อความเฉพาะเจาะจงถึงผู้ใช้” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การส่งข้อความเฉพาะเจาะจงถึงผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ตัวดักจับ WebSocket
  2. การปรับแต่งตัวแปลงข้อความ
  3. การจัดการเซสชันผู้ใช้
  4. การส่งข้อความเฉพาะเจาะจงถึงผู้ใช้
← กลับไปที่ WebSockets & Real-Time Systems with Spring