WebSocket – Create Spring WebSocket Application with SpringBoot + SockJS + STOMP
Websocket
3
springboot
164
stomp
2
sockjs
2
Male avatar

loveprogramming viết ngày 06/05/2021

https://grokonez.com/spring-framework/spring-websocket/create-spring-websocket-application-springboot-sockjs-stomp

WebSocket – Create Spring WebSocket Application with SpringBoot + SockJS + STOMP

The WebSocket protocol provides new capability for web applications: full-duplex, two-way communication. So in the system where the client and server need to exchange data at high frequency and with low latency, WebSocket is the best solution. That is the reason for us to create a Spring WebSocket Application with JavaSampleApproach.

Related Post: Spring Boot WebSocket with Angular 5 Client | SockJS + STOMP

I. Spring WebSocket Application

1. Flow of messages

We create a Spring WebSocket Application with the below flow of messages:

spring-websocket-architecture-ws

Detail explanations:

  • WebSocket clients connect to the WebSocket endpoint at '/jsa-stomp-endpoint'
  • Subscriptions to '/topic/hi' pass through the response channel, then are forwarded to the In-memory broker (Simple Broker).
  • User objects sent to '/jsa/hello' pass through the request channel then are forwarded to the spring WebController. WebController will handle User objects by @MessageMapping and transform to Hello messages then use @SendTo returns the messages to '/topic/hi' through the brokerChannel.

...

@MessageMapping("/hello")
@SendTo("/topic/hi")
public Hello greeting(User user) throws Exception {
    return new Hello(...);
}

...
  • The Simple Broker broadcasts messages to subscribers through the response channel.

    2. Server side

    In server side, we use SockJS and STOMP for our application.

What is SockJS?
-> SockJS lets applications use a WebSocket API but falls back to non-WebSocket alternatives when necessary at runtime, without the need to change application code.

Simple Java configuration to enable SockJS and Stomp in Spring application:

More at:

https://grokonez.com/spring-framework/spring-websocket/create-spring-websocket-application-springboot-sockjs-stomp

WebSocket – Create Spring WebSocket Application with SpringBoot + SockJS + STOMP

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 7 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 7 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...
loveprogramming viết 4 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!