Tiger
Tiger

Reputation: 31

Stomp subscribe callback is not working Spring MVC

I was using Spring MVC(4.2.2.RELEASE) and Tomcat 8. My requirement is to send a notification to the browser. Please find the code below.

Controller -------------- @Controller public class MessageController { //@Autowired private SimpMessagingTemplate template; @Autowired private SimpMessageSendingOperations template; List noteList = new ArrayList();

    public MessageController() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }  
    //@SubscribeMapping("/topic/notify")
    //@SendToUser("/topic/notify")
    public void sendMessage(String msg){
        Notification note = new Notification();
        note.setMsg(msg);
        noteList.add(note);
        template.convertAndSend("/topic/price", noteList);
    }
}


Configuration
---------------
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

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

    }

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

}

Client code
---------
var socket = new SockJS("/intl-fcstone/ws");
    var stompClient = Stomp.over(socket);
    var connectCallback = function() {
        console.log('----inside connectCallback before----');
        stompClient.subscribe('/topic/price', notifyUser);
        console.log('----inside connectCallback after----');
    };
    var errorCallback = function(error) {
        console.log('----inside errorCallback----');
        alert(error.headers.message);
    };
    stompClient.connect("guest", "guest", connectCallback, errorCallback);

    function notifyUser(frame) {
        console.log('----inside notifyUser1----'+frame);
        console.log('----inside notifyUser2----'+frame.body);
        var notes = JSON.parse(frame.body);
        console.log('----inside notifyUser3----'+notes);
        if(notes !=''){

            var $messageDiv = $('#notification_div'); // get the reference of the div
            $messageDiv.show().html(notes);
        }

Chrome console
---------------
Opening Web Socket...
stomp.js:134 Web Socket Opened...
stomp.js:134 >>> CONNECT
login:guest
passcode:guest
accept-version:1.1,1.0
heart-beat:10000,10000


stomp.js:134 <<< CONNECTED
version:1.1
heart-beat:0,0
user-name:[email protected]


stomp.js:134 connected to server undefined
(index):159 ----inside connectCallback before----CONNECTED
user-name:[email protected]
heart-beat:0,0
version:1.1


stomp.js:134 >>> SUBSCRIBE
id:sub-0
destination:/topic/price


(index):161 ----inside connectCallback after----CONNECTED
user-name:[email protected]
heart-beat:0,0
version:1.1

---------------------------

after this, nothing happens.

http://localhost:8080/intl-fcstone/ws/info shows

{
entropy: -646614392,
origins: [
"*:*"
],
cookie_needed: true,
websocket: true
}


Any help on this is much appreciated.

Upvotes: 3

Views: 3314

Answers (2)

Tiger
Tiger

Reputation: 31

This issue has been resolved. I added some lines to the context.xml

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/ws">
        <websocket:handshake-interceptors>
            <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor" />
        </websocket:handshake-interceptors>
        <websocket:sockjs session-cookie-needed="true" />
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic, /queue" />

    <websocket:client-inbound-channel>
        <websocket:executor core-pool-size="50"
                max-pool-size="100" keep-alive-seconds="60" />
    </websocket:client-inbound-channel>
    <websocket:client-outbound-channel>
        <websocket:executor core-pool-size="50"
                max-pool-size="100" keep-alive-seconds="60" />
    </websocket:client-outbound-channel>
    <websocket:broker-channel>
        <websocket:executor core-pool-size="50"
                max-pool-size="100" keep-alive-seconds="60" />
    </websocket:broker-channel>
</websocket:message-broker>

and used

@Autowired private SimpMessageSendingOperations template; 

in my controller. Also I used

template.convertAndSendToUser(authentication.getName(), "/queue/price", noteList);

which is sending a notification to the specific user.

And finally, at the client side

user = frame.headers['user-name'];
stompClient.connect(user, user, connectCallback, errorCallback);

Upvotes: 0

Charnjeet Singh
Charnjeet Singh

Reputation: 3107

Please follow this Spring WebSocket

Upvotes: 0

Related Questions