I want to connect a client on a server to receive a Flux of some entity and keep then connected (In a non-blocking + Assynchronous fashion) in order to receive updates or new insertions in the same Flux.
In other words I want that DB inform the server when there are some update or new data. The server to inform the client. All in a assynchronous + non-blocking fashion.
I tried with spring-boot-starter-webflux, with plain http, also tried spring-boot-starter-rsocket, with rsocket.
Tried with redis, mongo and now postgresql.
The only way it worked almost as expected was with MongoDB capped colelction + tailable stream. But in this way the collection is capped and I can’t edit an existing entry.
Are you aware about some approach to accomplish that?
Thank you very much!
2
Answers
Use the spring-boot-starter-rsocket, create a MessageMapping endpoint and the server listen to a topic with reactive-redis, thus there are message data channel.
Postgres includes a Notifier/Listener pattern to track the messages on database, for example.
The whole example is here.
As you mentioned, with Mongo capped collections, it is easy to emit the item to a reactive Flux sink, check my fullstack(frontend+backend) example of Http/SSE, WebSocket, RSocket.
You can emit any data to a connnectable flux by your own logic, such as emitting data by fine-grained domain events, this usage is more generic in real world projects.
Any of your client can connect to this
commentAddedEvent
. For example, the following is using SSE.Similarly, if you are using WebSocket, use a WebSocketHandler to adapt it, and for RSocket, use a controller with messaging mapping route instead.