What does this websocket api means? - spring-boot

I have created spring boot websocket project. I want to know what does
localhost:9090/gs-guide-websocket/parm1/param2/websocket
This Api means?
here i know 'gs-guide-websocket' is my websocket end point but what is the rest of all?
WebSocketConfig.java
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").withSockJS();
}
Controller.java
#MessageMapping("/hello")
#SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
}
In the about Url what is the sender identity and whom we are sending?
Code is working fine.

The #MessageMapping annotation ensures that if a message is sent to destination "/hello", then the greeting() method is called.
After the 1 second delay, the greeting() method creates a Greeting object and returns it. The return value is broadcast to all subscribers to "/topic/greetings" as specified in the #SendTo annotation.
Refer This Example

Related

There is an endpoint connection error problem implementing the websocket with spring boot

This is my WebSocketConfig.
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/pub");
registry.enableSimpleBroker("/sub");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket").setAllowedOriginPatterns("*").withSockJS();
}
}
This is my SocketController:
#Controller
public class SocketController {
#MessageMapping("/comm/message")
#SendTo("/sub/test")
public Message message(Message message) {
return message;
}
}
I am testing the socket through Chrome's Web Socket Test Client and Advanced Rest Client, but my connection endpoint ws://localhost:8080/socket returns the error.
Is there anything I set wrong?
connecting try tool and endPoint
return message

How to send message to WebSocket client from Spring WebSocket server using STOMP?

I have two Spring Boot WebSocket applications using STOMP:
WebSocket server
WebSocket client
I am able to send a WebSocket message from the client and respond to it from the server. However, now I would like to send a WebSocket message to the client triggered by an event on the server side.
Can someone tell me a way to do this?
Here is what I have now on the server side:
WebSocketConfig.java:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic/");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/alarm");
}
}
WebSocketController.java:
#Controller
public class WebSocketController {
#MessageMapping("/alarm")
#SendTo("/topic/message")
public void processMessageFromClient(#Payload String message, Principal principal) throws Exception {
System.out.println("WEBSOCKET MESSAGE RECEIVED" + message);
}
#RequestMapping(value = "/start/{alarmName}", method = RequestMethod.POST)
public String start(#PathVariable String alarmName) throws Exception {
System.out.println("Starting " + alarmName);
/* SEND MESSAGE TO WEBSOCKET CLIENT HERE */
return "redirect:/";
}
}
I found the answer on the official spring documentation.
You just need to inject a SimpMessagingTemplate.
My controller now looks like this:
#Controller
public class WebSocketController {
private SimpMessagingTemplate template;
#Autowired
public WebSocketController(SimpMessagingTemplate template) {
this.template = template;
}
#MessageMapping("/alarm")
#SendTo("/topic/message")
public void processMessageFromClient(#Payload String message, Principal principal) throws Exception {
System.out.println("WEBSOCKET MESSAGE RECEIVED" + message);
}
#RequestMapping(value = "/start/{alarmName}", method = RequestMethod.POST)
public String start(#PathVariable String alarmName) throws Exception {
System.out.println("Starting " + alarmName);
this.template.convertAndSend("/topic/message", alarmName);
return "redirect:/";
}
}

Large messages failing on Stomp Controller in Spring Boot

I have a stomp controller in a spring boot application, When ever i send a message which exceeds 256kb it fails to enter the controller. I don't see any error messages. Is there any setting where I can configure it to allow larger messages.
Here is my controller
#Component
#Controller
public class DiscussionController {
#MessageMapping("/discussion")
public void post(DiscussionMessage message) {
}
}
Here is my config file
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{
private final Logger log = LoggerFactory.getLogger(WebSocketConfig.class);
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("topic");
config.setApplicationDestinationPrefixes("ngdesk");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ngdesk-websocket").setAllowedOrigins("*").withSockJS();
}
}
You need to configure the web-socket transport, e.g.:
#Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
registration.setMessageSizeLimit(512 * 1024); // 512K
}

Getting Spring simpMessagingTemplate to work with websocket

I have been trying to get simpMessagingTemplate to send to websocket in Spring but to no avail. From what I can see of related stackoverflow posts and other guides, I have provided the necessary configuration and mapping of paths.
My code is shown as below:
RestController (which I use to invoke sending of the message to the websocket):
#RestController
public class RestControllers {
#Autowired
private SimpMessagingTemplate template;
#RequestMapping("/test")
public String doTest() {
Message m = new Message();
m.setFrom("foo");
m.setText("bar");
template.convertAndSend("/app/chat/test-topic", m);
return m.toString();
}
}
Controller:
#Controller
public class ChatController
{
#MessageMapping("/chat/{topic}")
#SendTo("/topic/messages")
public OutputMessage send(#DestinationVariable("topic") String topic,
Message message) throws Exception
{
System.out.println("THE MESSAGE WAS RECEIVED:" + message.toString());
return new OutputMessage(message.getFrom(), message.getText(), topic);
}
}
Configuration:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer
{
#Override
public void configureMessageBroker(MessageBrokerRegistry config)
{
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) //?? alternative only?
{
registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();
}
}

Spring Stomp #SubscribeMapping("/user/...") with User Destination doesn't work

I need to react on a user destination subscription.
Example:
A user subscribes to /user/messages, because he wants to receive all incoming messages. Now I'd like to look up any messages for this user, which were created while he was offline, and then send them to that user.
Working code:
Client code:
stompClient.subscribe('/user/messages', function(msg){
alert(msg.body);
});
Server code:
template.convertAndSendToUser(p.getName(), "/messages", "message content");
What I need:
It seems like it's not possible to catch an user destination subscription on server side, i.e.:
#SubscribeMapping("/user/messages")
public void test(Principal p) {
sendMessagesThatWereReceivedWhileUserWasOffline();
}
What I tried:
#SubscribeMapping("/messages")
public void test(Principal p) { ... }
This works, if the client subscribes to /app/messages, but it won't get called for /user/messages.
My Configuration:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp").withSockJS();
}
#Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/queue", "/topic");
registry.setUserDestinationPrefix("/user");
}
#Override
public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
return true;
}
// all other methods left empty
}
Using Spring 4.1.
I can't imagine that this isn't possible. What have I missed / done wrong?
Thank you :)
Define the user prefix also as an application prefix, and you'll then be able to map the subscription in your controller. Configuration:
#Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app", "/user");
registry.enableSimpleBroker("/queue", "/topic");
registry.setUserDestinationPrefix("/user");
}
Controller:
#SubscribeMapping("/messages")
public void test(Principal p) {
sendMessagesThatWereReceivedWhileUserWasOffline();
}

Resources