Sockjs Stomp subscribe not working

860 views
Skip to first unread message

rossi19...@gmail.com

unread,
Jan 17, 2017, 8:28:28 AM1/17/17
to sockjs

I want to use websocket to send from server to client something.. Here's my code:

---- Websocket configuration on server ----

@Configuration
@EnableWebSocketMessageBroker
@Slf4j
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{

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

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

---- Controller---

@Controller
@Component
@Slf4j
public class MenuItemController{
    @SendTo("/topic/notification")
    public String sendToMenuItems(MenuItemDto menuItem)  {
        return menuItem.getMenuItemName();
    }
}

----- Client ----

var SockJS = require('sockjs-client');
var Stomp = require('stompjs'); 

let connectWebSocket = () => {
  socket = new SockJS(context.backend + '/myWebSocketEndPoint');
  stompClient = Stomp.over(socket);
  stompClient.connect("","",function (frame) {
    console.log(frame);
    stompClient.subscribe('/topic/notification', function(menuItem){
        alert(menuItem);
    });
  });
}
connectWebSocket();

I use context.backend because i use webpack, so websocket connect over the backend server. It works for connect and also subscribe seems working, but when i send something from server to client, the alert doesn't appear and in console there are no message incoming by websocket. What's my problem?

Reply all
Reply to author
Forward
0 new messages