0Pricing
WebSockets & Real-Time Systems with Spring · レッスン

特定ユーザーへのターゲットメッセージング

ユーザー宛先、@SendToUser、SimpMessagingTemplate.convertAndSendToUserを使って、STOMPでユーザーごとのプライベートメッセージを送信する方法を学びます。

「特定ユーザーへのターゲットメッセージング」はCoddyKit上の無料WebSockets & Real-Time Systems with Springレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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

よくある質問

「特定ユーザーへのターゲットメッセージング」レッスンは無料ですか?

はい。「特定ユーザーへのターゲットメッセージング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebSockets & Real-Time Systems with Springコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。

「特定ユーザーへのターゲットメッセージング」で何を学びますか?

ユーザー宛先、@SendToUser、SimpMessagingTemplate.convertAndSendToUserを使って、STOMPでユーザーごとのプライベートメッセージを送信する方法を学びます。 ブラウザで直接実行するハンズオンコードでWebSockets & Real-Time Systems with Springを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

WebSockets & Real-Time Systems with Springを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWebSockets & Real-Time Systems with Springは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン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に戻る