7

I have the following contoller

@Controller
public class GreetingController 
{
        @MessageMapping("/hello")
        @SendTo("/topic/greetings")
        public Person greeting(String message) throws Exception {
                Person person=new Person();
                person.setAge(10);
                return person;
        }
        @Autowired
        private SimpMessagingTemplate template;

        @RequestMapping(path="/meeting",method=RequestMethod.POST)
        public  @ResponseBody void greet() {
            this.template.convertAndSend("/topic/greetings", "message");
         }
    }

and my configuration is

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig1 extends AbstractWebSocketMessageBrokerConfigurer {

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

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

So according to spring doc template.convertAndSend("/topic/greetings", "message") should call the broker and the mapped web socket will be called.

Code for front-end using SockJS

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

                         });
    // to send via the web service-WORKING ( but websocket not called in springs)
     $.post("http://localhost:8080/meeting");

    // to send via websocket - WORKING
    stompClient.send("/app/hello", {}, JSON.stringify({ 'message':'message'}));

There are no errors in the console. I can connect it through SockJs and send message to "/topic/greetings" but i want to call a webService which in return calls the web Socket. So after searching alot i m stuck cause there are no errors and cant find a different way to do it in spring.

Afroz Shaikh
  • 362
  • 4
  • 18
  • Be sure that your clients are subscribed to the `/topic/greetings`. Otherwise there is nothing on the server side to send message to. – Artem Bilan Aug 12 '16 at 14:49
  • 1
    I make connection and subscribe to the client using sockjs. And m able to even send it using sockjs. But if I call web service /meeting from postman after I made connection in sockjs nothing happens – Afroz Shaikh Aug 12 '16 at 20:01
  • Well, not sure how to help you. Everything looks good. I Spring Integration we have a similar test-case: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/inbound/StompInboundChannelAdapterWebSocketIntegrationTests.java. The `StompInboundChannelAdapter` does a `subscribeDestination(destination);` and there is exactly `messagingTemplate.send("/topic/myTopic",`. So, please, investigate DEBUG logs of your application or just debug your `greet()` method up to the Spring source code – Artem Bilan Aug 12 '16 at 21:22
  • Maybe your `HelloMessage` just can't be converted properly to `byte[]` for sending. See `AbstractMessageBrokerConfiguration.brokerMessageConverter()` – Artem Bilan Aug 12 '16 at 21:22
  • @AfrozShaikh Did you figure this out? – Basil Nov 28 '17 at 01:32
  • The javascript code in the description is incomplete, so I am not sure. Are you calling the `$.post` method after the `stompClient.connect`´s callback is executed? Have you tried to put breakpoints in the browser to check that the callback is actually executed before the $.post? – Francois Gergaud Feb 04 '18 at 07:54
  • 5
    @AfrozShaikh how did you resolve this? I have same issue, annotation works but not via the messaging template – mns Mar 20 '18 at 05:37
  • @AfrozShaikh your code is perfect. Only one thing you can try is try removing ResponseBody from your greet() method. – Swaprks Dec 05 '18 at 18:21

1 Answers1

0

I ran your code and instead of sending simple string you should send object so instead of

this.template.convertAndSend("/topic/greetings", "message");

should be

this.template.convertAndSend("/topic/greetings", messageObject);

and your object should have some method to access content like

MessageObject.getConent();

otherwise you can always use toString()

and your js should be

stompClient.subscribe('/topic/greetings', function(greeting){
    console.log(JSON.parse(greeting.body).content);
});

notice content on the end of received object

PLASMA chicken
  • 2,777
  • 2
  • 15
  • 25
mariubog
  • 1,498
  • 15
  • 16