Spring WebFlux with socket.io capabilities - socket.io

I'm playing around with Spring WebFlux and how to do async I/O in Java but it seems I need to set up a lot of boilerplates for managing websockets. For example, very common operations need to be re-implemented by hand:
websocket connect/disconnect
message broadcasting (to a list or all connected sockets)
gathering websockets in rooms
Is there any library (mature or in-development) to abstract complexity of managing websockets with
Spring WebFlux ? I'm looking for similar capabilities/API of the wonderful socket.io.
I took a (very brief) look at RSocket, it looks promising but still at a lower-level than socket.io for NodeJS.
Any help/guidelines would be greatly appreciated.

Related

How to consume external websocket API in Apache Camel since ahc-ws deprecate?

ahc and ahc-ws (Async Http Client) components have been deprecated in Apache camel version 3.16: https://issues.apache.org/jira/browse/CAMEL-17667.
Is there an alternative for ahc-ws? The component was very easy to use to consume external websockets API.
Other libraries like Jetty, Undertow, Atmosphere, don't seem to offer this kind of features. I have not been able to configure them and the documentation remains unclear. They only provide the server part.
For the websocket-jsr356 component, I can't configure the component to consume a WebSockets over SSL API (wss). The library seems to support only classic websocket (ws).
I looked for alternatives on the camel doc, examples on github but I didn't find anything.
Is there a viable alternative to ahc-ws to consume external websocket APIs simply with camel?
Thanks a lot
Looks like it's not deprecated yet. There is just a suggestion for that. ahc-wss is very useful currently and there is no viable alternative for the same. websocket component requires tedious tweaking of secure storage parameters and is just kills the purpose of wss. I hope they don't deprecate ahc-wss without a proper replacement though.

STOMP/Websocket architecture in Spring with decoupling between client and broker

I was searching for some STOMP/Websocket examples with Spring and I found a lot of chats or news applications like this:
I would like to know why the client is coupled with the broker (look at the arrow from Request channel to Simple broker) instead of interacting only with the Spring application. Also, beyond the coupling between client and the broker technology, I think that this architecture allows the client to publish in the broker bypassing the application (that, for instance, maybe it would like to implement some spam filter).
In some sense, this is like using the endpoints of some application and at the same time be able to query the application's database. It looks like an antipattern and I cannot find any advantage, only disadvantages like no decoupling and security issues.
I saw a lot of architectures using STOMP/Websocket designed in this way so probably I am not seeing something.
Thanks in advance.

Difference between websocket and vertx

I've been recently learning about vertx and websocket.
What I understood that you can have real-time application using them, but I didn't understand the difference between them
A websocket lets you push messages from server to client and vice-versa, encouraging the publish-subscribe methodology. Then what is vertx doing? It acts as an event bus and also encourages publish-subscribe model.
I can be completely wrong in my analysis, hence please correct me and I'll be glad. thanks.
WebSockets is a stateful protocol, usually used to communicate between clients and servers.
Vert.x is a server-side framework, which has a very good support for WebSockets.
You don't have to use WebSockets with Vert.x. But if you do want to use WebSockets, Vert.x is a great option.
Here's an article I wrote a few months ago about both:
https://medium.com/#alexey.soshin/playing-ping-pong-over-websockets-with-vert-x-447c634c6c87

How to design reactive microservices, which have external blocking API calls?

I have some microservices, which should work on top of WebFlux framework. Each server has own API with Mono or Flux. We are using MongoDB, which is supported by Spring (Spring Data MongoDb Reactive).
The problem is external blocking API, which I have to use in my system.
I have one solution. I can just wrap blocking API calls in dedicated thread pool and use it with CompletableFuture.
Is there anything else to solve my problem? I think, that brand new Rsocket cannot solve my problem.
1.If possible, you can change your blocking API call to the reactive way using the WebClient class.
References:
Reference guide
WebClient API
A simple, complete sample
2.If the blocking API can't be changed to reactive ones, we should have a dedicated, well-tuned thread pool and isolate the blocking code there.
There is also an example here.
I don't see why you cannot wrap a blocking API call in a Flux or a Mono. You can also integrate Akka with Spring if the actor model seems easier to you.
RSocket should be a perfect fit, good tutorials to get you started
https://www.baeldung.com/spring-boot-rsocket
https://spring.io/blog/2020/04/06/getting-started-with-rsocket-spring-boot-channels

Does Spring Boot with its Blocking IO really fit well with Microservices?

There are a lot of tutorials and articles (including official site) promoting spring boot as a good tool for building microservices.
Let's say we have some rest api endpoint (User profile) which aggregates data from multiple services (User service, Stat service, Friends service).
To achieve this, user profile endpoint makes 3 http calls to those services.
But in Spring, requests are blocking and as I see, the server will quickly run out of available resources (threads) to serve request in such system.
So to me, it as quite inefficient way to build such systems (compared to non-blocking frameworks, like play! framework or node.js)
Do I miss something?
P.S.: I do not mean here spring 5 with its new webflux framework.
No one prevents you from building an asynchronous microservice architecture with Spring Boot :).
Something along these lines:
Instead of one service calling another synchronously, a service can put events to a queue (e.g. RabbitMQ). The events are delivered to services that subscribe to those events.
Using RabbitMQ and its "exchange" concept, the event producing service doesn't even need to the consumers of its events.
A blog post detailing this with Spring Boot code can be found here: https://reflectoring.io/event-messaging-with-spring-boot-and-rabbitmq/
This is not a limitation of Spring rather it is more to do with the Application Architecture.
For instance, the scenario that you have is commonly solved using Aggregate Design Pattern
While this solution is quite prevalent,it has the limitation of being synchronous, and thus blocking. Asynchronous behaviour in such scenarios should be implemented in an application specific way.
Having said that if you have to call other services in order to be able to serve a response to a request from a client(outside), this is typically an architectural problem. It really doesn’t matter if you are using HTTP or asynchronous message passing (with a request-reply pattern), the overall response time for the outside client will be bad
Also, I have seen quite a few applications which uses synchronous REST calls for external clients, but when communication is needed between internal MicroServices, it should always be asynchronous. You can read an interesting paper on this topic here MicroServices Messaging Patterns

Resources