3

I want to implement a websocket inside my projet to do something as notifications

this is my configuration web socket in my projet spring boot

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

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

and in my controller I have this method :

@MessageMapping("/hello")
    @SendTo("/topic/templatesWS")
    public void TemplatesWebSocket() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("calling juste");
    }

and in my client side in my page index.html I added this

<script src="//cdn.jsdelivr.net/sockjs/1/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.js"></script>

and in my component angular2 I'm using this

declare let SockJS;
        declare let Stomp;


        var socket = new SockJS('http://localhost:8080/hello');
        socket.ono
        var stompClient = Stomp.over(socket);
            stompClient.connect({}, function(frame) {
            console.log('Connected: ' + frame);
            stompClient.subscribe('http://localhost:8080/template/topic/templatesWS', function(greeting) {
                console.log("from from", greeting);
            });
        }, function (err) {
            console.log('err', err);
        });

How can I call the method TemplatesWebSocket()

enter image description here

3
  • I think the subscribe() parameter should be simply '/topic/templatesWS' Commented Aug 22, 2016 at 13:48
  • How send data to server Or get and object after an event ? Commented Aug 22, 2016 at 14:04
  • I resolve this error I have some configuration websocket security that prevent connection but the line code stompClient.subscribe not call the method from controller TemplatesWebSocket() how can I call it Commented Aug 22, 2016 at 14:26

1 Answer 1

1

Check this example I just uploaded: https://github.com/mtrojahn/spring-boot-websocket

I think what you meant is send a command to the server and get a response, right? The example above covers both the response to a command sent by the client and also periodic messages sent to the clients.

You are probably looking for something like this:

stompClient.client.send("/app/somecommand", {}, "");

I hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

the send method it works fine but stompClient.subscribe not working why we use the subscribe method ?? when we subscribe in a method with scheduled is for getting for example data every 20 secondes but in case with MessageMapping if you can explain that to me thanks (I updat the question I add the picture of subscribe method error)
Websockets is about subscribing to a service and wait for data. The server pushes (broadcast) data to all subscribed clients. The 20s example shows that happening, every 20s your client will receive a message. Your problem with the subscribe method being undefined sounds like a problem with the order the scripts are being loaded by the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.