This guide walks you through the process of creating a “hello world” application that sends messages back and forth, between a browser and the server. WebSocket is a very thin, lightweight layer above TCP. It makes it very suitable to use “subprotocols” to embed messages. In this guide we’ll dive in and use STOMP messaging with Spring to create an interactive web application.
What you’ll build
You’ll build a server that will accept a message carrying a user’s name. In response, it will push a greeting into a queue that the client is subscribed to.
What you’ll need
How to complete this guide
Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
To start from scratch, move on to Build with Gradle.
To skip the basics, do the following:
Download and unzip the source repository for this guide, or clone it using Git:
git clone https://github.com/spring-guides/gs-messaging-stomp-websocket.git
Jump ahead to Create a resource representation class.
When you’re finished, you can check your results against the code in
Build with Gradle
Build with Maven
Build with your IDE
Create a resource representation class
Now that you’ve set up the project and build system, you can create your STOMP message service.
Begin the process by thinking about service interactions.
The service will accept messages containing a name in a STOMP message whose body is a JSON object. If the name given is “Fred”, then the message might look something like this
To model the message carrying the name, you can create a plain old Java object with a
nameproperty and a corresponding
Upon receiving the message and extracting the name, the service will process it by creating a greeting and publishing that greeting on a separate queue that the client is subscribed to. The greeting will also be a JSON object, which might look something like this: