I am trying to build a micro service using web-flux which will send/publish some data based on an event for a particular subscriber.
With the below implementation (Another Stackflow Issue) I am able to create one publisher and all who are subscribed will receive the data automatically when we trigger the event by calling "/send" API
@SpringBootApplication
@RestController
public class DemoApplication {
final FluxProcessor processor;
final FluxSink sink;
final AtomicLong counter;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
public DemoApplication() {
this.processor = DirectProcessor.create().serialize();
this.sink = processor.sink();
this.counter = new AtomicLong();
}
@GetMapping("/send/{userId}")
public void test(@PathVariable("userId") String userId) {
sink.next("Hello World #" + counter.getAndIncrement());
}
@RequestMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent> sse() {
return processor.map(e -> ServerSentEvent.builder(e).build());
}
}
Problem statement - My app is having user based access and for each user there will be some notifications which I want to push only based on an event. Here the events will be stored in the DB with user id's and when we hit the "send" end point from another API along with "userId" as a path variable, it should only send the data related to that user only if it is registered as a subscriber and still listening on the channel.