How to send message to client through websocket using Spring

I was able to solve my problem thanks to @Boris the Spider. The correct solution is to do something like that : @Controller @RequestMapping(“https://stackoverflow.com/”) public class PhotoController { @Autowired private SimpMessagingTemplate template; @MessageMapping(“/form”) @SendTo(“/topic/greetings”) public Greeting validate(AddPhotosForm addPhotosForm) { FireGreeting r = new FireGreeting( this ); new Thread(r).start(); return new Greeting(“Hello world !”); } public … Read more

Spring WebSocket Connecting with SockJS to a different domain

Jax’s answer was correct 🙂 The registerStompEndpoints method gives us the opportunity to set the Allowed Origins. We need to add it before the “withSockJs()” option. @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint(“/BO/socket”).setAllowedOrigins(“*”).withSockJS(); }

Engine.io or SockJS, which one to choose?

Have you looked at Primus? It offers the cookie requirements you mention, it supports all of the major ‘real-time’/websocket libraries available and is a pretty active project. To me it also sounds like vendor lock-in could be a concern for you and Primus would address that. The fact that it uses a plugin system should … Read more

Websocket in Spring Boot app – Getting 403 Forbidden

I had a similar issue and fixed it in the WebSocketConfig by setting the allowed origins to “*”. @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // the endpoint for websocket connections registry.addEndpoint(“/stomp”).setAllowedOrigins(“*”).withSockJS(); } // remaining config not shown as not relevant }

Where “user” comes from in convertAndSendToUser works in SockJS+Spring Websocket?

We know we can send messages to the client from a stomp server using the topic prefixes that he is subscribed to e.g. /topic/hello. We also know we can send messages to a specific user because spring provides the convertAndSendToUser(username, destination, message) API. It accepts a String username which means if we somehow have a … Read more

How to unmarshal an escaped JSON string

You might want to use strconv.Unquote on your JSON string first 🙂 Here’s an example, kindly provided by @gregghz: package main import ( “encoding/json” “fmt” “strconv” ) type Msg struct { Channel string Name string Msg string } func main() { var msg Msg var val []byte = []byte(`”{\”channel\”:\”buu\”,\”name\”:\”john\”, \”msg\”:\”doe\”}”`) s, _ := strconv.Unquote(string(val)) err … Read more

Header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’

Problem: You are not configuring ‘Access-Control-Allow-Origin’ correctly and your current configuration is simply ignored by the server. Situation: The Error stack trace says: The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’. Origin ‘http://localhost:4200’ is therefore not allowed access. It means that … Read more

Path variables in Spring WebSockets @SendTo mapping

Even though @MessageMapping supports placeholders, they are not exposed / resolved in @SendTo destinations. Currently, there’s no way to define dynamic destinations with the @SendTo annotation (see issue SPR-12170). You could use the SimpMessagingTemplate for the time being (that’s how it works internally anyway). Here’s how you would do it: @MessageMapping(“/fleet/{fleetId}/driver/{driverId}”) public void simple(@DestinationVariable String … Read more

JSON Web Token (JWT) with Spring based SockJS / STOMP Web Socket

Current Situation UPDATE 2016-12-13 : the issue referenced below is now marked fixed, so the hack below is no longer necessary which Spring 4.3.5 or above. See https://github.com/spring-projects/spring-framework/blob/master/src/docs/asciidoc/web/websocket.adoc#token-authentication. Previous Situation Currently (Sep 2016), this is not supported by Spring except via query parameter as answered by @rossen-stoyanchev, who wrote a lot (all?) of the Spring … Read more