Why isnt my sockets onsubscribe event getting used? - spring-boot

I am using java springboot with maven in order to get the spring boot starter socket package. My clients are using angular with stompjs and sockjs-client.
I am trying to set up a simple web socket application that allows for multiple rooms based on a roomId. When a client joins a room they should receive the last five messages sent in that room.
My Springboot app has three classes, the basic Application.java that I use to run the app, a web socket config class and a web socket controller:
#Controller
public class WebSocketController {
private final SimpMessagingTemplate template;
#Autowired
WebSocketController(SimpMessagingTemplate template){
this.template = template;
}
#MessageMapping("/meeting/{roomId}")
private void sendMessageTpPrivateRoom(
String message,
#DestinationVariable String roomId
) throws IOException {
System.out.println("message sent to: " + roomId);
this.template.convertAndSend("/meeting/" + roomId, message);
addToHistory(roomId, message);
}
#SubscribeMapping("/meeting/{roomId}")
public String chatInit(#DestinationVariable String roomId) {
System.out.println("Someone joined room: " + roomId);
return getLastFiveMessages(roomId);
}
}
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfiguration
extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
#Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app")
.enableSimpleBroker("/meeting");
}
}
my clients are subscribing to the socket like so:
stompClient.subscribe(`app/meeting/${roomId}`, (message) => {
if (message.body) {
console.log(message.body);
messages += '<br>' + message.body;
}
});
and sending messages like so:
this.stompClient.send(`/app/meeting/${this.roomId}` , {}, message);
The message sending and handling is working great, when I set up three clients, two in room one, and one in room two, the room two messages are not being seen in room one and the room one messages are seen by both clients.
However the on subscribe event is not firing no matter what room I join. It is very necessary that when a client joins room one, they should receive some sort of history of that room. Any advice as to why my SubscribeMapping method is not being triggered when a client subscribes to the room?

The /meeting part will be implicitly added to URL you provide when subscribing. So your mapping will look like this:
#SubscribeMapping("/${roomId}")
public String chatInit(#DestinationVariable String roomId) {
System.out.println("Someone joined room: " + roomId);
return getLastFiveMessages(roomId);
}
Source: https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/websocket.html

Related

How do I throttle the amount of data sent to Stomp queue (handling websockets) so that I can guarantee that I don't overflow the buffer?

I have two Java processes and I am connecting them using a websocket in spring boot. One process acts as the client and connects like this:
List<Transport> transports = new ArrayList<Transport>(1);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
WebSocketClient client = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
StompSessionHandler firstSessionHandler = new MyStompSessionHandler("Philip");
stompClient.connect("ws://localhost:8080/chat", firstSessionHandler);
The session handler extends StompSessionHandlerAdapter and provides these methods (I am subscribing by username so each client can receive its own messages):
#Override
public void afterConnected(
StompSession session, StompHeaders connectedHeaders) {
session.subscribe("/user/" + userName + "/reply", this);
session.send("/app/chat", getSampleMessage());
}
#Override
public void handleFrame(StompHeaders headers, Object payload) {
Message msg = (Message) payload;
// etc.....
}
On the server side I have a Controller exposed and I am writing data by calling the endpoint from a worker thread.
#Autowired
private SimpMessagingTemplate template;
#MessageMapping("/chat")
public void send(
Message message)
throws Exception {
template.convertAndSendToUser(message.getFrom(),
"/reply",
message);
}
In the websocket config I am overriding the method to set the limits:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic", "/user");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
registration.setMessageSizeLimit(500 * 1024);
registration.setSendBufferSizeLimit(1024 * 1024);
registration.setSendTimeLimit(20000);
}
My question is this, if the load on the server gets high enough and I overrun the limit, the websocket fails catastrophically, and I want to avoid this. What I would like to do is for the controller to have the ability to ask the message broker "will this message fit in the buffer?", so that I can throttle to stay under the limit. I searched the API documentation but I don't see any way of doing that. Are there any other obvious solutions that I am missing?
Thanks.
Actually I found a solution, so if anyone is interested, here it is.
On the server side configuration of the websockets I installed an Interceptor on the Outbound Channel (this is part of the API), which is called after each send from the embedded broker.
So I know how much is coming in, which I keep track of in my Controller class and I know how much is going out through the Interceptor that I installed, and this allows me to always stay under the limit.
The controller, before accepting any new messages to be queued up for the broker first determines if enough room is available and if not queues up the message in external storage until such time as room becomes available.

Spring creating dashboard using web sockets for live updates

The front page of my web app is a dashboard, where I pull out information such as:
Latest entered clients, 5 top male clients, and female and also some calculated statistics.
The approach I went with first, was creating endpoints in my controller that would calculate such things and return a JSON file with the results, and then make an Ajax call and put the data in the html file. But I was told the best way to do this, would be to use Web Sockets, considering that when another users puts a client in, simultaniously he needs to show up on the dashboard.
Following a tutorial what I have so far is this :
public class MyMessageHandler extends TextWebSocketHandler {
#Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// The WebSocket has been closed
}
#Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// The WebSocket has been opened
// I might save this session object so that I can send messages to it outside of this method
// Let's send the first message
session.sendMessage(new TextMessage("You are now connected to the server. This is the first message."));
}
#Override
protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) throws Exception {
// A message has been received
System.out.println("Message received: " + textMessage.getPayload());
}
}
And the websocket endpoint :
#Configuration
#EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {
#Bean
public WebSocketHandler myMessageHandler() {
return new MyMessageHandler();
}
#Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myMessageHandler(), "/my-websocket-endpoint");
}
}
But this tutorial shows how a message will be sent-received real time.
My controller has and endpoint like this:
#RequestMapping(value="getDashboard/{gender}",method=RequestMethod.GET)
public #ResponseBody List<Client> getTopClients(#PathVariable("gender")
char gender) {
return clientService.findTop5ByGenderOrderByResult_DataResults_ScoreDesc(gender);
}
What I'm trying to understand is, where do this service so that it is called as long as the connection is open, so if there's any update, it updates the dashboard real time.
I guess what I'm trying to understand is the skeleton, of what this would look like using web sockets.
Just an overview of its logic/example.
Any help is appreciated and I apologize as I'm a beginner, I dont fully understand how to switch from what I had done before, and I'm trying to learn best practices.

WebSocket dynamically add and remove Endpoints

I have created this Websocket project Spring Websocket and it works really fine.
I will introduce this example in my project. There I have the requirement that (chat-) groups can dynamically be created or removed/destroyed.
In my WebsocketConfig- class endpoints can be added statically by:
registry.addEndpoint("/hello").withSockJS(); (also see below)
Is there any possibility to add endpoints dynamically?
My usecase is that I have companies and employees which belong to one or more companies:
n m (m:n relation)
company <--------> employees
and companies can be created dynamically (by clicking a button "create"). Then employees, which registered before can be added to company.
So this means that if a company is created (and minimim 2 employees are added to company) than an endpoint should be added.
I would be glad for any helpful answer in this direction.
Thanks a lot!
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
// Prefix for messages FROM server TO client
config.enableSimpleBroker("/topic");
// Prefix for messages FROM client TO server
config.setApplicationDestinationPrefixes("/app");
// /app wird beim client - sendName verwendet: stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name
// }));
}
#Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}
[Edit]
Send message to more than one client but not to all. This is my current code below. Send to all with the same id works fine but I don't know how to send Message to e.g. 4 clients.
Thanks for help!
#MessageMapping("/chat/{institutionId}")
public void greeting(#DestinationVariable String institutionId, final GreetingHelloMessage message) throws Exception {
final Greeting greeting = new Greeting(institutionId, "Hello " + institutionId + " - " + message.getName());
simpMessagingTemplate.convertAndSend("/topic/chat/" + institutionId, greeting);
}
You should have a look in the direction of path parameters.
There is no need for using differnt endpoints for every chat if you can use a construct like localhost:8080/chat/{GROUP_NAME}.

Multiple rooms in Spring using STOMP

Is it possible to create rooms with STOMP and Spring 4? Socket.IO has rooms built in, so I'm wondering if Spring has this
My code at the moment:
#MessageMapping("/room/greet/{room}")
#SendTo("/room/{room}")
public Greeting greet(#DestinationVariable String room, HelloMessage message) throws Exception {
return new Greeting("Hello, " + room + "!");
}
It would be ideal to have #SendTo("/room/{room}")
however, I am limited to:
#SendTo("/room/room1")
#SendTo("/room/room2")
#SendTo("/room/room3")
etc...which is VERY VERY unideal
The client is:
stompClient.subscribe('/room/' + roomID, function(greeting){
showGreeting(JSON.parse(greeting.body).content);
});
where roomID can be room1, room2, or room3...
What If I want more rooms? It feels like such a pain right now
It looks like this "room" feature is actually a publish/subscribe mechanism, something achieved with topics in Spring Websocket support (see STOMP protocol support and destinations for more info on this).
With this example:
#Controller
public class GreetingController {
#MessageMapping("/room/greeting/{room}")
public Greeting greet(#DestinationVariable String room, HelloMessage message) throws Exception {
return new Greeting("Hello, " + message.getName() + "!");
}
}
If a message is sent to "/room/greeting/room1", then the return value Greeting will be automatically sent to "/topic/room/greeting/room1", so the initial destination prefixed with "/topic".
If you wish to customize the destination, you can use #SendTo just like you did, or use a MessagingTemplate like this:
#Controller
public class GreetingController {
private SimpMessagingTemplate template;
#Autowired
public GreetingController(SimpMessagingTemplate template) {
this.template = template;
}
#MessageMapping("/room/greeting/{room}")
public void greet(#DestinationVariable String room, HelloMessage message) throws Exception {
Greeting greeting = new Greeting("Hello, " + message.getName() + "!");
this.template.convertAndSend("/topic/room/"+room, greeting);
}
}
I think taking a quick look at the reference documentation and some useful examples, such as a portfolio app and a chat app should be useful.
You can use netty socket the implementation of socket io in java

How can I send a message on connect event (SockJS, STOMP, Spring)?

I am connection through SockJS over STOMP to my Spring backend. Everything work fine, the configuration works well for all browsers etc. However, I cannot find a way to send an initial message. The scenario would be as follows:
The client connects to the topic
function connect() {
var socket = new SockJS('http://localhost:8080/myEndpoint');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/notify', function(message){
showMessage(JSON.parse(message.body).content);
});
});
}
and the backend config looks more or less like this:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketAppConfig extends AbstractWebSocketMessageBrokerConfigurer {
...
#Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/myEndpoint").withSockJS();
}
I want to send to the client an automatic reply from the backend (on the connection event) so that I can already provide him with some dataset (e.g. read sth from the db) without the need for him (the client) to send a GET request (or any other). So to sum up, I just want to send him a message on the topic with the SimMessagingTemplate object just after he connected.
Usually I do it the following way, e.g. in a REST controller, when the template is already autowired:
#Autowired
private SimpMessagingTemplate template;
...
template.convertAndSend(TOPIC, new Message("it works!"));
How to achieve this on connect event?
UPDATE
I have managed to make it work. However, I am still a bit confused with the configuration. I will show here 2 configurations how the initial message can be sent:
1) First solution
JS part
stompClient.subscribe('/app/pending', function(message){
showMessage(JSON.parse(message.body).content);
});
stompClient.subscribe('/topic/incoming', function(message){
showMessage(JSON.parse(message.body).content);
});
Java part
#Controller
public class WebSocketBusController {
#SubscribeMapping("/pending")
Configuration
#Override
public void configureMessageBroker(final MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
... and other calls
template.convertAndSend("/topic/incoming", outgoingMessage);
2) Second solution
JS part
stompClient.subscribe('/topic/incoming', function(message){
showMessage(JSON.parse(message.body).content);
})
Java part
#Controller
public class WebSocketBusController {
#SubscribeMapping("/topic/incoming")
Configuration
#Override
public void configureMessageBroker(final MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
// NO APPLICATION PREFIX HERE
}
... and other calls
template.convertAndSend("/topic/incoming", outgoingMessage);
SUMMARY:
The first case uses two subscriptions - this I wanted to avoid and thought this can be managed with one only.
The second one however has no prefix for application. But at least I can have a single subscription to listen on the provided topic as well as send initial message.
If you just want to send a message to the client upon connection, use an appropriate ApplicationListener:
#Component
public class StompConnectedEvent implements ApplicationListener<SessionConnectedEvent> {
private static final Logger log = Logger.getLogger(StompConnectedEvent.class);
#Autowired
private Controller controller;
#Override
public void onApplicationEvent(SessionConnectedEvent event) {
log.debug("Client connected.");
// you can use a controller to send your msg here
}
}
You can't do that on connect, however the #SubscribeMapping does the stuff in that case.
You just need to mark the service method with that annotation and it returns a result to the subscribe function.
From Spring Reference Manual:
An #SubscribeMapping annotation can also be used to map subscription requests to #Controller methods. It is supported on the method level, but can also be combined with a type level #MessageMapping annotation that expresses shared mappings across all message handling methods within the same controller.
By default the return value from an #SubscribeMapping method is sent as a message directly back to the connected client and does not pass through the broker. This is useful for implementing request-reply message interactions; for example, to fetch application data when the application UI is being initialized. Or alternatively an #SubscribeMapping method can be annotated with #SendTo in which case the resulting message is sent to the "brokerChannel" using the specified target destination.
UPDATE
Referring to this example: https://github.com/revelfire/spring4Test how would that be possible to send anything when the line 24 of the index.html is invoked: stompClient.subscribe('/user/queue/socket/responses' ... from the spring controllers?
Well, look like this:
#SubscribeMapping("/queue/socket/responses")
public List<Employee> list() {
return getEmployees();
}
The Stomp client part remains the same.

Resources