Skip to content

Repository files navigation

STOMP over WebSocket implementation in Java

This is a STOMP protocol implementation.

Example backend using Spring Boot

The following example is taken from Spring's "Using WebSocket to build an interactive web application" guide but with SockJS disabled.

Configuration

@Configuration@EnableWebSocketMessageBrokerpublicclassWebSocketConfigextendsAbstractWebSocketMessageBrokerConfigurer {
@OverridepublicvoidconfigureMessageBroker(MessageBrokerRegistryconfig) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@OverridepublicvoidregisterStompEndpoints(StompEndpointRegistryregistry) {
registry.addEndpoint("/gs-guide-websocket");
}
}

Controller

@ControllerpublicclassGreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
publicGreetinggreeting(HelloMessagemessage) throwsException {
Thread.sleep(1000); // simulated delayreturnnewGreeting("Hello, " + message.getName() + "!");
}
}

Usage

See the included client example module.

finalStompClientstompSocket = newStompClient(URI.create("ws://localhost:8080/gs-guide-websocket"));
// Wait for a connection to establishbooleanconnected;
try {
connected = stompSocket.connectBlocking();
} catch (InterruptedExceptione) {
e.printStackTrace();
return;
}
if (!connected) {
System.out.println("Failed to connect to the socket");
return;
}
// Subscribing to a topicstompSocket.subscribe("/topic/greetings", newStompMessageListener() {
@OverridepublicvoidonMessage(Stringmessage) {
System.out.println("Server message: " + message);
// DisconnectstompSocket.close();
}
});
// Sending JSON message to a serverStringmessage = "{\"name\": \"Jack\"}";
stompSocket.send("/app/hello", message);

About

STOMP 1.2 over WebSocket implementation in Java

Topics

Resources

Stars

14 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages