I am using Spring boot 2.6.3. This is a Spring webflux project and using RSocket over Websoocket for Notification.
Server side:-
build.gradle,
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-messaging'
implementation 'org.springframework.security:spring-security-rsocket'
implementation 'org.springframework.boot:spring-boot-starter-rsocket'
Controller,
@Controller
public class RSocketController {
private static final List<RSocketRequester> CLIENTS = new ArrayList();
@ConnectMapping
void onConnect(RSocketRequester rSocketRequester) {
rSocketRequester.rsocket().onClose().doFirst(() -> {
CLIENTS.add(rSocketRequester);
}).doOnError(error -> {
}).doFinally(consumer -> {
}
}).subscribe();
return Mono.empty();
}).subscribe();
rSocketRequester.route("client-status").data("Success!").send().subscribe();
}
@MessageMapping("send-notification")
public void sendNotification(Mono<String> notification) {
notification.subscribe(message -> {
LOGGER.info("" + message);
}, e -> LOGGER.error(e.getMessage(), e), () -> LOGGER.info("completed without a value."));
}
@GetMapping("/notify")
public void giveCallback() {
CLIENTS.get(0).route("callback-notification").data("CALLBACK TEST").send().subscribe();
}
}
Client Side:-
class ClientHandler {
@MessageMapping("client-status")
public void statusUpdate(String status) {
System.out.println("Called from server ========== -> Connection {}" + status);
}
@MessageMapping("callback-notification")
public void requestResponse(String callback) {
System.out.println("Received callback from server, request: " + callback);
}
}
@Controller
public class ClientController {
public ClientController(@Autowired RSocketRequester.Builder rsocketRequesterBuilder, RSocketStrategies strategies) {
String client = UUID.randomUUID().toString();
SocketAcceptor responder = RSocketMessageHandler.responder(strategies, new ClientHandler());
this.rSocketRequester = rsocketRequesterBuilder
.rsocketStrategies(strategies).rsocketConnector(connector -> {
connector.acceptor(responder);
})
.websocket(URI.create("ws://localhost:9091/rsocket/"));
this.rSocketRequester.route("send-notification").data("Test Send").send().subscribe();
}
}
Here, From the client,
- The client is able to connect to the server
- The "connect-mapping" method gets invoked on a new connection on the server side and it sends status message to the client and I am able to see the message on the client side.
- The "callback-notification" method on the server side is able to send notification to the client and I am able to see the message on the client side.
But, Calling send-notification on the client side is not executing sendNotification method on server side.
this.rSocketRequester.route("send-notification").data("TEST Server Notification").send().subscribe();
Here, the client responders are working. But, the server responders are not working. Is there any special config required for the server responders.