Not able to configure Stomp and SockJS endpoint in Spring MVC.Connection issue !!!

See original GitHub issue

I am implementing Notification System. And want to initialize Socket connection when user Logged In, and show him his notifications, and also if some event happens.

My Code snippet as follows.

websocket.js :

var stompClient = null;
function connect( temp ) {
    alert(temp);
    //var socket = new SockJS("/websock");
    //var socket = new SockJS("/websock"+temp);
    var socket = new SockJS(context_path+"/websock"+temp);
    //context_path == "/SupportCenter"
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function( frame ){
        console.log( "Connected :- "+frame );
        stompClient.subscribe("/topic/notifications", function( notifications ) {
            alert( notifications );
        });
    }, function( error ) {
        alert( error );
    });
    alert();
    getNotifications();
}

function getNotifications() {
    stompClient.send("/app/hello", {}, "Hiiiiii");
}

WebSocketConfig.java :

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry.addEndpoint("/websock").withSockJS();
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // TODO Auto-generated method stub
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }
}

WebSocketController.java :

@Controller
public class WebSocketController {

    @MessageMapping(value="/hello")
    @SendTo("/topic/notifications")
    public Notify hello() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Notify notify = new Notify();
        notify.setMessage("Hello World !!!");
        return notify;
    }
}

Some code Hom.jsp :

<script type="text/javascript" src="<c:url value="/resources/js/sockjs.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/resources/js/stomp.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/resources/js/websocket.js"/>"></script>


<script type="text/javascript">
$(document).ready(function() {
    //...

    connect( '${nsec}');
});

Why Firefox Console giving XML Parsing Error: no root element found Location: while in Network tab status code is 200 OK. ? What is the cause for this, any suggestions?

Versions I’m using Spring-MVC - 4.1.6 STOMP - 1.7.1 SockJS-client - 1.1.4

Console TAB
websocket_connection_error6

Network TAB
websocket_connection_error5

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ShantaramTupecommented, Dec 4, 2017

solved this by

function connect( temp ) {
    alert(temp);
    //var socket = new SockJS("/websock");
    //var socket = new SockJS("/websock"+temp);
    var socket = new SockJS(context_path+"/websock"+temp);
    //context_path == "/SupportCenter"
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function( frame ){
        console.log( "Connected :- "+frame );
        stompClient.subscribe("/topic/notifications", function( notifications ) {
            alert( notifications );
        });
    getNotifications();//************Changed Here
    }, function( error ) {
        alert( error );
    });
}
0reactions
Sonia-devcommented, Jul 11, 2021

Invalid SockJS path ‘/topic/public’ - required to have 3 path segments same error !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stomp over socket using sockjs can't connect with Spring 4 ...
When i finished migration - i tried websockets - and it is works! I thought that the main problem was in XML configuration...
Read more >
25. WebSocket Support - Spring
Section 25.4.2, “Enable STOMP over WebSocket” demonstrates how to configure STOMP ... However, Spring's WebSocket support does not depend on Spring MVC.
Read more >
Using Spring Boot for WebSocket Implementation with STOMP
Let's look at how to implement WebSockets with the Spring Boot framework, and use STOMP for effective client-server communication.
Read more >
Intro to WebSockets with Spring - Baeldung
As we've seen, Spring's approach to working with STOMP messaging is to associate a controller method to the configured endpoint.
Read more >
WebSockets With Spring, Part 3: STOMP Over WebSocket
To connect to a broker, a client sends a CONNECT frame with two mandatory headers: accept-version — the versions of the STOMP protocol...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found