Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/endpoint").withSockJS();
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
and message controller
MessageController.java
@Controller
public class MessageController {
@MessageMapping("/topic")
@SendTo("/topic/greetings/{message}")
public String handleMessage(@PathVariable String message){
return "[" + message + "] at " + LocalDate.now();
also my javascript to handle websocket client
app.js
var stompClient = null;
function connect() {
var socket = new SockJS('/endpoint');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings/asd', function (message){
console.log("Message received: " + message)
function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
$("#greetings").html("");
function disconnect() {
if (stompClient !== null) {
stompClient.disconnect();
setConnected(false);
console.log("Disconnected");
function sendName() {
stompClient.send("/app/topic", {}, $("#name").val());
$(function () {
$("form").on('submit', function (e) {
e.preventDefault();
$( "#connect" ).click(function() { connect(); });
$( "#disconnect" ).click(function() { disconnect(); });
$( "#send" ).click(function() { sendName(); });
and html page
index.html
<!DOCTYPE html>
<title>Hello WebSocket</title>
<link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/main.css" rel="stylesheet">
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
<script src="/app.js"></script>
</head>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
enabled. Please enable
Javascript and reload this page!</h2></noscript>
<div id="main-content" class="container">
<div class="row">
<div class="col-md-6">
<form class="form-inline">
<div class="form-group">
<label for="connect">WebSocket connection:</label>
<button id="connect" class="btn btn-default" type="submit">Connect</button>
<button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
</button>
</form>
<div class="col-md-6">
<form class="form-inline">
<div class="form-group">
<label for="name">What is your name?</label>
<input type="text" id="name" class="form-control" placeholder="Your name here...">
<button id="send" class="btn btn-default" type="submit">Send</button>
</form>
</body>
</html>
So every message that comes at /app/topic
should be sent to /topic/gretings/{message}
but it's not. I have an error:
java.lang.IllegalArgumentException: Could not resolve placeholder 'message' in value "/topic/greetings/{message}"
.
I've read some other articles and people were using @DestinationVariable
instead of @PathVariable
but then I have other error:
org.springframework.messaging.MessageHandlingException: Missing path template variable 'message' for method parameter type [class java.lang.String]
.
The point of this is to client subscribe his own channel and exchange data with other client which will be desktop application in C#.
As you send the message in the body with stompClient.send("/app/topic", {}, $("#name").val());
you should use @RequestBody
instead of @PathVariable
.
To get the content of the posted message:
@MessageMapping("/topic")
public String handleMessage(@RequestBody String message){
Next to send after enrichment to the topic /topic/greetings
you could use:
@SendTo("/topic/greetings")
Putting everything together will get message from /app/topic
and send to subscribers of /topic/greetings
@MessageMapping("/topic")
@SendTo("/topic/greetings")
public String handleMessage(@RequestBody String message){
return "[" + message + "] at " + LocalDate.now();
If you like to use @PathVariable
you should map send the message with :
stompClient.send("/app/topic/" + $("#name").val() , {}, {});
and get it defining mapping like :
@MessageMapping("/topic/{message}")
@SendTo("/topic/greetings")
public String handleMessage(@PathVariable String message){
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.