Search code examples
springstomprest-clientspring-websocketsockjs

How to invoke Spring STOMP web socket method from rest client


My Java Code which intercepts all the calls with the URL /hello (Back) :

@Controller
public class GreetingController {

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(3000); // simulated delay
        return new Greeting("Hello, " + message.getName() + "!");       
    }    
}

My JavaScript code which calls the method greeting() (Front) :

function sendName() {
    var name = document.getElementById('name').value;
    stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
}

Can some one please let me know how can I invoke @MessageMapping("/hello") method from Rest client. I can able to connect through JavaScript code and even getting response and displaying it in HTML. But I have a requirement where external system(through rest call) should invoke this @MessageMapping("/hello") and then application(backend) should send response to HTML. I tried to invoke @MessageMapping("/hello") method from chrome rest client plugin but was unsuccessful.

Please let me know how to invoke this method? Thanks in advance


Solution

  • @RestController
    @RequestMapping("/rest")
    public class GreetingController {
    
        private SimpMessagingTemplate template;
    
          @Autowired
            public GreetingController(SimpMessagingTemplate template) {
                this.template = template;
            }
    
    
        @RequestMapping(value="/hello", method=RequestMethod.POST)  
        public String greeting(@RequestBody HelloMessage message) throws Exception {
    
            this.template.convertAndSend("/topic/greetings", message.getName()+"asgdasd");  
            return "hello";
        }    
    }
    

    using SimpMessagingTemplate i achived this and my javascript code using sockjs which will be subscribe to /topic/greetings is

    <script type="text/javascript">
            var stompClient = null;
    
            function setConnected(connected) {
                document.getElementById('connect').disabled = connected;
                document.getElementById('disconnect').disabled = !connected;
                document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
                document.getElementById('response').innerHTML = '';
            }
    
            function connect() {
                var socket = new SockJS('/hello');
                stompClient = Stomp.over(socket);            
                stompClient.connect({}, function(frame) {
                    setConnected(true);
                    console.log('Connected: ' + frame);
                    stompClient.subscribe('/topic/greetings', function(greeting){
                        alert("inside subscribe");
                        alert(greeting);
                        showGreeting(JSON.parse(greeting.body).content);
                    });
                });
            }
    
            function disconnect() {
                if (stompClient != null) {
                    stompClient.disconnect();
                }
                setConnected(false);
                console.log("Disconnected");
            }
    
    /*         function sendName() {
                var name = document.getElementById('name').value;
                stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
            } */
    
            function showGreeting(message) {
                var response = document.getElementById('response');
                var p = document.createElement('p');
                p.style.wordWrap = 'break-word';
                p.appendChild(document.createTextNode(message));
                response.appendChild(p);
            }
        </script>