การผสาน WebSockets เข้ากับ Spring
กำหนดค่าการรองรับ WebSocket ในแอปพลิเคชัน Spring Boot เพื่อเปิดใช้การเชื่อมต่อถาวรระหว่างไคลเอ็นต์กับเซิร์ฟเวอร์สำหรับการสมัครรับข้อมูล
การผสาน WebSockets เข้ากับ Spring เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
WebSockets for Real-time Data
Welcome! In this lesson, we'll integrate WebSockets with Spring Boot. This is crucial for enabling real-time features like GraphQL Subscriptions.
WebSockets provide a persistent, two-way communication channel between a client and a server over a single, long-lived TCP connection.
Why WebSockets for Subscriptions?
Unlike traditional HTTP requests (which are short-lived), GraphQL Subscriptions require a continuous connection to push data updates to clients as they happen.
WebSockets are the perfect fit because they maintain an open connection, allowing the server to send data to the client at any time without a new request.
Adding the Dependency
First, we need to add the Spring Boot WebSocket starter dependency to our project. This brings in all the necessary libraries to enable WebSocket support.
For Gradle, add this to your build.gradle:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-websocket'
}Enabling WebSocket Message Broker
In Spring, we use annotations to enable specific functionalities. For WebSockets with a message broker, we use @EnableWebSocketMessageBroker.
This annotation enables a STOMP over WebSocket message broker, which is a powerful way to handle messaging.
Configuring WebSocket Endpoints
We need a configuration class that extends WebSocketMessageBrokerConfigurer. This class allows us to customize our WebSocket setup.
The registerStompEndpoints() method is used to define the URL endpoint clients will connect to for WebSocket communication.
Adding SockJS Fallback
When registering an endpoint, we often add .withSockJS(). SockJS is a JavaScript library that provides a WebSocket-like API.
It offers fallback options (like HTTP streaming or long polling) for browsers that don't fully support WebSockets or in environments with proxies that interfere.
Configuring the Message Broker
The configureMessageBroker() method sets up the message broker itself. This defines how messages are routed between clients and the application.
enableSimpleBroker("/topic"): Enables an in-memory message broker for destinations prefixed with/topic. Clients subscribe to these topics.setApplicationDestinationPrefixes("/app"): Defines prefixes for messages destined for methods annotated with@MessageMappingin our application.
Complete WebSocket Configuration
Here's a minimal Spring Boot application demonstrating the WebSocket configuration. Try running it to see Spring initialize the WebSocket server!
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 Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
System.out.println("WebSocket server ready on port 8080!");
System.out.println("Connect at ws://localhost:8080/ws-connect");
}
@Configuration
@EnableWebSocketMessageBroker
static class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-connect")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
}
}Bridging to GraphQL Subscriptions
With this WebSocket infrastructure, our GraphQL subscription resolvers (from the previous lesson) can now publish events.
When a resolver publishes an event, Spring's message broker ensures that all clients subscribed to the relevant /topic are immediately notified via their open WebSocket connection.
Quick Check: WebSocket Config
Consider the following line from our WebSocket configuration:
config.enableSimpleBroker("/topic");What is the primary purpose of the /topic prefix in this context?
Recap: WebSockets in Spring
Great job! You've learned how to integrate WebSockets with Spring Boot to provide a robust real-time communication layer.
- We added the WebSocket starter dependency.
- We used
@EnableWebSocketMessageBrokerandWebSocketMessageBrokerConfigurer. - We configured STOMP endpoints (e.g.,
/ws-connectwith SockJS). - We set up a message broker with prefixes like
/topicfor subscriptions and/appfor app-specific messages.
This setup is the foundation for delivering real-time GraphQL Subscriptions!
เรียนรู้ GraphQL APIs with Spring Boot ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การผสาน WebSockets เข้ากับ Spring” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสาน WebSockets เข้ากับ Spring” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสาน WebSockets เข้ากับ Spring”
กำหนดค่าการรองรับ WebSocket ในแอปพลิเคชัน Spring Boot เพื่อเปิดใช้การเชื่อมต่อถาวรระหว่างไคลเอ็นต์กับเซิร์ฟเวอร์สำหรับการสมัครรับข้อมูล คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสาน WebSockets เข้ากับ Spring” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม
ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจการสมัครรับข้อมูล GraphQL
- การนำการอัปเดตแบบเรียลไทม์มาใช้
- การผสาน WebSockets เข้ากับ Spring
- การกรองและการขยายขนาดการสมัครรับข้อมูล