Home  >  Article  >  Create a Flux that can publish arbitrary events in the future

Create a Flux that can publish arbitrary events in the future

PHPz
PHPzforward
2024-02-09 11:50:24495browse

php editor Apple will introduce you to an exciting development tool-Flux. Flux is an application architecture for building user interfaces that focuses on the concept of unidirectional data flow, making state management simple and predictable. By creating a Flux architecture, developers can easily manage the state of their applications and be able to publish arbitrary events in the future, enabling more flexible application development. Flux's simplicity and extensibility make it ideal for modern front-end development. Next, we’ll dive into how Flux works and how to use it to build great user interfaces.

Question content

I want to create a custom flux that can publish arbitrary events in the future.

For example:

@RestController
public class EventController {
    @GetMapping(path = "/event/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> eventStream() {
       // how to create a flux here which I can publish arbitrary events to in future?
    }
}

I saw the flux.from(...) and flux.generate(...) methods in the flux javadocs, but these don't seem to meet my needs

Solution

I ended up using sinks.many()

Many<String> emitter = Sinks.many().unicast().onBackpressureBuffer();
Flux<String> flux = emitter.asFlux();

I can do it now

The above is the detailed content of Create a Flux that can publish arbitrary events in the future. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete