{"id":3536,"date":"2025-09-22T10:00:46","date_gmt":"2025-09-22T14:00:46","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3536"},"modified":"2025-09-22T10:00:46","modified_gmt":"2025-09-22T14:00:46","slug":"spring-websocket-building-real-time-web-applications","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring_sockets\/spring-websocket-building-real-time-web-applications\/","title":{"rendered":"Spring WebSocket: Building Real-Time Web Applications"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>Spring WebSocket simplifies the development of real-time, bidirectional communication between web browsers and servers. By leveraging WebSocket technology, you can build interactive applications like chat apps, real-time dashboards, or collaborative tools.<\/p>\n<h3>Setting up your Project<\/h3>\n<h4>Gradle<\/h4>\n<pre><code class=\"language-groovy\">implementation 'org.springframework.boot:spring-boot-starter-websocket'\n<\/code><\/pre>\n<h4>Maven<\/h4>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-websocket&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<h3>WebSocket Configuration<\/h3>\n<pre><code class=\"language-java\">@Configuration\n@EnableWebSocketMessageBroker\npublic class WebSocketConfig implements WebSocketMessageBrokerConfigurer {\n\n    @Override\n    public void configureMessageBroker(MessageBrokerRegistry config) {\n        \/\/ Enable a simple memory-based message broker to carry messages back to the client on destinations prefixed with &quot;\/topic&quot;.\n        config.enableSimpleBroker(&quot;\/topic&quot;);\n        \/\/ Designate the &quot;\/app&quot; prefix to filter destinations targeting application annotated methods.\n        config.setApplicationDestinationPrefixes(&quot;\/app&quot;);\n    }\n\n    @Override\n    public void registerStompEndpoints(StompEndpointRegistry registry) {\n        \/\/ Registers the &quot;\/gs-guide-websocket&quot; endpoint, enabling SockJS fallback options so that alternate transports may be used if WebSocket is not available.\n        registry.addEndpoint(&quot;\/gs-guide-websocket&quot;).withSockJS();\n    }\n}\n<\/code><\/pre>\n<h3>Simple Example with Data Flow Explanation<\/h3>\n<p><strong>Controller:<\/strong><\/p>\n<pre><code class=\"language-java\">@Controller\npublic class GreetingController {\n\n    @MessageMapping(&quot;\/hello&quot;)  \/\/ 1. Client sends message to &quot;\/app\/hello&quot;\n    @SendTo(&quot;\/topic\/greetings&quot;) \/\/ 3. Server sends greeting to &quot;\/topic\/greetings&quot;\n    public String greet(String message) throws Exception {\n        Thread.sleep(1000); \/\/ Simulate processing time\n        return &quot;Hello, &quot; + message + &quot;!&quot;; \/\/ 2. Server processes the message\n    }\n}\n<\/code><\/pre>\n<p><strong>Client-Side (JavaScript):<\/strong><\/p>\n<pre><code class=\"language-javascript\">var stompClient = null;\n\nfunction connect() {\n    var socket = new SockJS('\/gs-guide-websocket'); \/\/ Connect to the WebSocket endpoint\n    stompClient = Stomp.over(socket);\n    stompClient.connect({}, function (frame) {\n        console.log('Connected: ' + frame);\n        stompClient.subscribe('\/topic\/greetings', function (greeting) { \/\/ 4. Client listens on &quot;\/topic\/greetings&quot;\n            showGreeting(JSON.parse(greeting.body).content);\n        });\n    });\n}\n\nfunction sendName() {\n    stompClient.send(&quot;\/app\/hello&quot;, {}, JSON.stringify({'name': $(&quot;#name&quot;).val()})); \n}\n\nfunction showGreeting(message) {\n    $(&quot;#greetings&quot;).append(&quot;&lt;tr&gt;&lt;td&gt;&quot; + message + &quot;&lt;\/td&gt;&lt;\/tr&gt;&quot;);\n}\n<\/code><\/pre>\n<h3>Data Flow:<\/h3>\n<ol>\n<li><strong>Client to Server:<\/strong> The client sends a message to the server through the WebSocket connection, targeting the destination <code>\/app\/hello<\/code>.<\/li>\n<li><strong>Server Processing:<\/strong> The <code>@MessageMapping<\/code> annotation on the <code>greet()<\/code> method in the <code>GreetingController<\/code> handles this message. It processes the message (e.g., adding &quot;Hello, &quot; to the message).<\/li>\n<li><strong>Server to Client (Broadcast):<\/strong> The <code>@SendTo<\/code> annotation designates the destination <code>\/topic\/greetings<\/code> where the processed message (\u201cHello, [message]!\u201d) should be sent. All subscribed clients receive this greeting.<\/li>\n<li><strong>Client Receives:<\/strong> The client, which has subscribed to <code>\/topic\/greetings<\/code>, receives the message from the server and displays it on the web page.<\/li>\n<\/ol>\n<h3>Key Points<\/h3>\n<ul>\n<li><strong>@EnableWebSocketMessageBroker:<\/strong> Enables WebSocket message handling and configures a message broker.<\/li>\n<li><strong>@MessageMapping:<\/strong> Maps a message to a controller method.<\/li>\n<li><strong>@SendTo:<\/strong> Specifies the destination where the response message should be sent.<\/li>\n<li><strong>configureMessageBroker:<\/strong> Configures the message broker to handle messages with the \u201c\/topic\u201d prefix and directs application messages to methods annotated with @MessageMapping.<\/li>\n<li><strong>registerStompEndpoints:<\/strong> Registers a WebSocket endpoint at \u201c\/gs-guide-websocket\u201d and enables SockJS as a fallback option.<\/li>\n<\/ul>\n<p>By understanding these concepts and building upon the provided examples, you can create powerful real-time web applications with Spring WebSocket.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3537,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[445],"tags":[69,319],"series":[397],"class_list":["post-3536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring_sockets","tag-java-2","tag-spring","series-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/intro-7400243_640.jpg?fit=640%2C334&ssl=1","jetpack-related-posts":[{"id":3632,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_sockets\/real-time-communication-with-spring-boot-websockets-a-comprehensive-guide\/","url_meta":{"origin":3536,"position":0},"title":"Real-Time Communication with Spring Boot WebSockets: A Comprehensive Guide","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"In the world of modern web applications, real-time communication has become a necessity. Whether it\u2019s live chat, collaborative editing, or real-time data updates, WebSockets have emerged as the go-to technology to enable seamless, bidirectional communication between the browser and server. In this article, we\u2019ll dive into how to harness the\u2026","rel":"","context":"In &quot;Spring Sockets&quot;","block_context":{"text":"Spring Sockets","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_sockets\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/cpu-4393380_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/cpu-4393380_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/cpu-4393380_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/cpu-4393380_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/cpu-4393380_1280-jpg.avif 3x"},"classes":[]},{"id":3548,"url":"https:\/\/www.mymiller.name\/wordpress\/spring\/beyond-the-basics-optimizing-your-spring-boot-applications-for-performance-fine-tune-your-application-for-speed-and-efficiency\/","url_meta":{"origin":3536,"position":1},"title":"Beyond the Basics: Optimizing Your Spring Boot Applications for Performance &#8211; Fine-tune your application for speed and efficiency.","author":"Jeffery Miller","date":"November 25, 2025","format":false,"excerpt":"Absolutely! Here\u2019s a blog article on optimizing Spring Boot applications, aimed at those who already have some experience with the framework: Beyond the Basics: Optimizing Your Spring Boot Applications for Performance Spring Boot is a fantastic framework for rapidly building production-ready applications. However, as your application grows and handles more\u2026","rel":"","context":"In &quot;Spring&quot;","block_context":{"text":"Spring","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/ai-generated-8619544_1280.jpg?fit=1200%2C685&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/ai-generated-8619544_1280.jpg?fit=1200%2C685&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/ai-generated-8619544_1280.jpg?fit=1200%2C685&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/ai-generated-8619544_1280.jpg?fit=1200%2C685&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/ai-generated-8619544_1280.jpg?fit=1200%2C685&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3935,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/%f0%9f%9a%80-dl4j-and-spring-boot-real-time-anomaly-detection-in-time-series-data\/","url_meta":{"origin":3536,"position":2},"title":"\ud83d\ude80 DL4J and Spring Boot: Real-Time Anomaly Detection in Time-Series Data","author":"Jeffery Miller","date":"November 25, 2025","format":false,"excerpt":"As a Software Architect, you understand that an excellent solution requires not just a powerful model, but a robust, scalable, and performant architecture for deployment. The combination of DL4J (Deeplearning4j) and Spring Boot is perfectly suited for building real-time, high-performance services, especially for a critical task like time-series anomaly detection.\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif 2x"},"classes":[]},{"id":3912,"url":"https:\/\/www.mymiller.name\/wordpress\/uncategorized\/spring-boot-4-0-whats-next-for-the-modern-java-architect\/","url_meta":{"origin":3536,"position":3},"title":"Spring Boot 4.0: What&#8217;s Next for the Modern Java Architect?","author":"Jeffery Miller","date":"September 24, 2025","format":false,"excerpt":"A Forward-Looking Comparison of Spring Boot 3.x and 4.0 Staying on top of the rapidly evolving Java ecosystem is paramount for any software architect. The shift from Spring Boot 2.x to 3.x brought significant changes, notably the move to Jakarta EE. Now, with the horizon of Spring Boot 4.0 and\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 3x"},"classes":[]},{"id":3919,"url":"https:\/\/www.mymiller.name\/wordpress\/spring\/unleashing-scalability-spring-boot-and-java-virtual-threads\/","url_meta":{"origin":3536,"position":4},"title":"Unleashing Scalability: Spring Boot and Java Virtual Threads","author":"Jeffery Miller","date":"November 18, 2025","format":false,"excerpt":"Java has long been a powerhouse for enterprise applications, and Spring Boot has made developing them an absolute dream. But even with Spring Boot's magic, a persistent bottleneck has challenged developers: the overhead of traditional thread-per-request models when dealing with blocking I\/O operations. Think database calls, external API integrations, or\u2026","rel":"","context":"In &quot;Spring&quot;","block_context":{"text":"Spring","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 3x"},"classes":[]},{"id":3931,"url":"https:\/\/www.mymiller.name\/wordpress\/uncategorized\/advanced-spring-ai-creating-agentic-workflows-with-function-calling\/","url_meta":{"origin":3536,"position":5},"title":"Advanced Spring AI: Creating Agentic Workflows with Function Calling","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"The landscape of AI is rapidly evolving, moving beyond simple request-response models to more sophisticated, agentic systems. These systems empower Large Language Models (LLMs) to not just generate text, but to act within your applications, making them an active and integral part of your business logic. Spring AI is at\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_kg5i0ykg5i0ykg5i.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_kg5i0ykg5i0ykg5i.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_kg5i0ykg5i0ykg5i.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_kg5i0ykg5i0ykg5i.avif 2x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3536"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3536\/revisions"}],"predecessor-version":[{"id":3538,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3536\/revisions\/3538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3537"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3536"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}