Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multiple messages to the same topic with Stomp

so I'm trying to send messages back to my browser from the server and have two methods that are supposed to be wired to send the messages to the STOMP topic.

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public static Greeting greeting(HelloMessage message) throws Exception {
    System.out.println("Sending message...");
    Application.startBody(message.getName());
    return new Greeting("Hello, " + message.getName() + "!");
}

@SendTo("/topic/greetings")
public static  Greeting security(String message) {
    System.out.println("entered the informer");
    return new Greeting("bye, " + message + "!");
}

The first one is also mapped to receive messages and send one back. The first funciton works and the message makes it back to the browser and displays on the webpage. The second once however, does not work. It never displays a received message in the console of the web page. Can I only send to the same topic with one method? I tried changing the topic and adding a subscription to my Stomp client but that didn't work either. Does it have anything to do with the second method being static? (I need to call it from a separate class.)

This is my subscription in the html file:

    function connect() {
        var socket = new SockJS("http://localhost:8080/hello");
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/greetings', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }

And here is my WebSocketConfig.java:

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.setApplicationDestinationPrefixes("/app").enableSimpleBroker("/queue","/topic");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}
like image 375
Theo Avatar asked Jul 08 '14 21:07

Theo


1 Answers

I have the same problem and reading the WebShocket Support of Spring it's said:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

STOMP servers can use the MESSAGE command to broadcast messages to all subscribers.

It’s important to know that a server cannot send unsolicited messages.*

All messages from a server must be in response to a specific client subscription and the "subscription-id" header of the server message must match the "id" header of the client subscription.

So I guess that the first method works because it's a response form the previous mapping.


Ok, I solved this problem with The SimpMessageSendingOperations

@Controller
public class PrincipalController {

private static SimpMessageSendingOperations messagingTemplate;

@Autowired
public PrincipalController(SimpMessageSendingOperations messagingTemplate) {
    this.messagingTemplate = messagingTemplate;
}

...

  public static void security(String message){
      System.out.println("entered the informer");
      PrincipalController.messagingTemplate.convertAndSend("/topic/greetings", new     Greeting("bye, " +    message + "!"));      
  }   
}
...
like image 174
Ramón Casares Avatar answered Sep 29 '22 11:09

Ramón Casares