Not able to configure Stomp and SockJS endpoint in Spring MVC.Connection issue !!!
See original GitHub issueI 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
Network TAB
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
solved this by
Invalid SockJS path ‘/topic/public’ - required to have 3 path segments same error !!!