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

SpringでのSTOMP設定

効率的な通信のために、SpringのSTOMPブローカーとメッセージ処理の仕組みを設定します。

「SpringでのSTOMP設定」はCoddyKit上の無料WebSockets & Real-Time Systems with Springレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWebSockets & Real-Time Systems with Spring学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Spring & STOMP Configuration

Spring Boot makes building real-time applications with WebSockets and STOMP much easier. It provides powerful abstractions and auto-configuration to get you started quickly.

In this lesson, we'll learn how to set up Spring to handle STOMP messages. This involves configuring message brokers, defining endpoints, and establishing communication paths.

Activating STOMP with Spring

The first step is to enable Spring's WebSocket message broker capabilities. This is done with a single annotation: @EnableWebSocketMessageBroker.

  • This annotation configures a message broker behind the scenes.
  • It also sets up a Spring application context for handling STOMP messages.
  • You'll typically place this on a configuration class.

Customizing Your STOMP Setup

To customize how Spring handles WebSocket and STOMP messages, you'll implement the WebSocketMessageBrokerConfigurer interface.

This interface provides methods that allow you to:

  • Configure the message broker.
  • Register STOMP endpoints.
  • Add interceptors and customize message converters.

Setting Up the Message Broker

The message broker routes messages to their intended recipients. Spring provides a "simple" in-memory broker for basic needs.

You configure it in the configureMessageBroker method:

  • enableSimpleBroker(): Activates the in-memory broker.
  • Paths like /topic are for public, publish-subscribe messages.
  • Paths like /queue are for private, point-to-point messages.

Simple Broker Example

Here's how to set up the basic in-memory message broker with /topic and /queue destinations. This will allow clients to subscribe to public topics and send private messages.

package com.coddykit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic", "/queue");
    }
    // Endpoints will be added later
}

Client-to-Server Paths

When a client sends a message to the server, it usually targets a specific server-side handler. We define a prefix for these messages using setApplicationDestinationPrefixes().

  • For example, if the prefix is /app, clients send messages to paths like /app/chat.sendMessage.
  • These messages are then routed to methods annotated with @MessageMapping on the server.

App Destination Prefix Example

Let's update our configuration to include an application destination prefix. This tells Spring which messages are intended for server-side methods.

package com.coddykit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic", "/queue");
        // Messages from clients for the server will start with /app
        config.setApplicationDestinationPrefixes("/app");
    }
    // Endpoints will be added later
}

Defining WebSocket Connection Point

Clients need a specific HTTP endpoint to initiate the WebSocket handshake. This is where they first connect before STOMP messaging begins.

You define this endpoint using registerStompEndpoints():

  • addEndpoint("/ws"): Makes the endpoint available at http://localhost:8080/ws.
  • withSockJS(): Enables SockJS fallback options for browsers that don't fully support WebSockets.

Complete STOMP Configuration

Here's the complete configuration class, combining the message broker setup, application destination prefix, and the STOMP endpoint registration. This is the foundation for your Spring STOMP application.

package com.coddykit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // Messages for client subscriptions (server to client)
        config.enableSimpleBroker("/topic", "/queue");
        // Messages from clients to server-side handlers
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // The HTTP endpoint for WebSocket handshake
        registry.addEndpoint("/ws").withSockJS();
    }
}

Configuration Check

You've learned about different prefixes in Spring STOMP configuration. It's crucial to understand their roles.

Summary of Configuration

You've successfully learned how to configure Spring for STOMP messaging! Here's a quick recap:

  • @EnableWebSocketMessageBroker: Activates WebSocket and STOMP support.
  • WebSocketMessageBrokerConfigurer: Interface for customizing the setup.
  • configureMessageBroker(): Sets up the message broker (e.g., enableSimpleBroker("/topic", "/queue")) and application destination prefixes (e.g., setApplicationDestinationPrefixes("/app")).
  • registerStompEndpoints(): Defines the WebSocket handshake URL (e.g., addEndpoint("/ws").withSockJS()).

These steps lay the foundation for building powerful real-time features with Spring and STOMP!

よくある質問

「SpringでのSTOMP設定」レッスンは無料ですか?

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

「SpringでのSTOMP設定」で何を学びますか?

効率的な通信のために、SpringのSTOMPブローカーとメッセージ処理の仕組みを設定します。 ブラウザで直接実行するハンズオンコードでWebSockets & Real-Time Systems with Springを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「SpringでのSTOMP設定」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWebSockets & Real-Time Systems with Springレッスンでコードを書いて実行できますか?

はい。すべてのWebSockets & Real-Time Systems with Springレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. STOMPプロトコルの概要
  2. SpringでのSTOMP設定
  3. STOMPメッセージの送受信
  4. Spring SecurityによるSTOMPエンドポイントの保護
← WebSockets & Real-Time Systems with Springに戻る