Streaming to Clients
Send what actors produce to browsers and services over Server-Sent Events, WebSocket or gRPC. Each bridge
builds on the messages Flux of Reactor Streams: a client gets a reference that
actors send to, and when the client disconnects, the reference stops. An actor that watches it receives
ActorTerminated and can forget the client or stop itself.
Incubating
actor-streams is incubating: its API may still change in a minor release, without a deprecation
period, while it collects feedback. Its types carry @Incubating. The compatibility promise covers
the core and cluster API.
All three need actor-streams:
| Transport | Stack | What you write |
|---|---|---|
| Server-Sent Events | Spring MVC or WebFlux | A controller method that returns the messages Flux |
| WebSocket | WebFlux | An ActorWebSocketHandler with a connector |
| gRPC | grpc-java or Spring gRPC | ActorGrpc.respond(flux, responseObserver) in the service method |
Server-Sent Events
Return the Flux from a controller with produces = text/event-stream. Spring MVC and WebFlux both write
each element as an event, and cancel the Flux when the client goes away:
import io.github.seonwkim.actor.SpringActorSystem;
import io.github.seonwkim.actor.streams.ActorStreams;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
public class PricesController {
private final ActorStreams streams;
private final SpringActorSystem actorSystem;
public PricesController(ActorStreams streams, SpringActorSystem actorSystem) {
this.streams = streams;
this.actorSystem = actorSystem;
}
@GetMapping(path = "/prices", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Price> prices() {
return Mono.fromCompletionStage(actorSystem.actor(TickerActor.class).withId("ticker").getOrSpawn())
.flatMapMany(ticker -> streams.messages(Price.class)
.bufferSize(16)
.overflow(ActorStreams.Overflow.DROP_OLDEST)
.flux(ref -> ticker.tell(new Subscribe(ref))));
}
}
import io.github.seonwkim.actor.SpringActorSystem
import io.github.seonwkim.actor.kotlin.actor
import io.github.seonwkim.actor.streams.ActorStreams
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
@RestController
class PricesController(
private val streams: ActorStreams,
private val actorSystem: SpringActorSystem,
) {
@GetMapping(path = ["/prices"], produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
fun prices(): Flux<Price> =
Mono.fromCompletionStage(actorSystem.actor<TickerActor, _>().withId("ticker").getOrSpawn())
.flatMapMany { ticker ->
streams.messages(Price::class.java)
.bufferSize(16)
.overflow(ActorStreams.Overflow.DROP_OLDEST)
.flux { ref -> ticker.tell(Subscribe(ref)) }
}
}
The ticker keeps its subscribers and forgets one when it stops, as shown in Reactor Streams.
When the client disconnects:
- WebFlux notices at once, and the reference stops.
- Spring MVC (a servlet container) notices only on its next write to that client. The reference stops after the next message the actor sends, which fails to write. If an actor may stay quiet for a long time, send a heartbeat now and then.
Use a small buffer with DROP_OLDEST for values where only the latest matters, so a slow client never
holds many messages.
WebSocket (WebFlux)
ActorWebSocketHandler is a WebFlux WebSocketHandler. For each connection it creates a client
reference and calls your connector, which connects it to actors and returns where the client's text frames
go:
@Configuration
public class WebSocketConfig {
@Bean
public HandlerMapping chatSocket(ActorStreams streams, SpringActorSystem actorSystem) {
ActorWebSocketHandler handler = new ActorWebSocketHandler(streams, (session, client) -> actorSystem
.actor(UserActor.class)
.withContext(new UserContext(UUID.randomUUID().toString(), client))
.spawn()
.thenApply(user -> text -> user.tell(new Incoming(text))));
return new SimpleUrlHandlerMapping(Map.of("/ws/chat", handler), 1);
}
}
@Configuration
class WebSocketConfig {
@Bean
fun chatSocket(streams: ActorStreams, actorSystem: SpringActorSystem): HandlerMapping {
val handler = ActorWebSocketHandler(streams) { _, client ->
actorSystem.actor<UserActor, _>()
.withContext(UserContext(UUID.randomUUID().toString(), client))
.spawn()
.thenApply { user -> Consumer<String> { text -> user.tell(Incoming(text)) } }
}
return SimpleUrlHandlerMapping(mapOf("/ws/chat" to handler), 1)
}
}
The user actor sends text to the browser with client.tell(json). To stop with the connection, it watches
the reference (context is the actor's own SpringActorContext subclass, which carries the client reference):
When the client disconnects, the reference stops and the user actor stops with it. The other way round,
when the outgoing Flux completes (see completeWhen below), the session closes.
The third constructor argument configures the outgoing messages, with the same settings as
messages:
By default the reference buffers 256 messages the client has not taken yet, and closes the session with an error when the buffer is full.
The chat example uses this bridge.
gRPC
ActorGrpc.respond sends a Flux (or a Mono, or any Reactive Streams Publisher) to a gRPC response
stream. Call it in a server-streaming or unary method of a grpc-java service, or of a Spring gRPC service:
@Service
public class PriceService extends PriceServiceGrpc.PriceServiceImplBase {
private final ActorStreams streams;
private final SpringActorHandle<TickerActor.Command> ticker; // spawned at startup, set in the constructor
@Override
public void watch(WatchRequest request, StreamObserver<Price> responses) {
ActorGrpc.respond(
streams.messages(Price.class).flux(ref -> ticker.tell(new Subscribe(ref))),
responses);
}
@Override
public void getPrice(PriceRequest request, StreamObserver<Price> responses) {
ActorGrpc.respond(streams.ask(ticker.ask(new GetPrice(request.getSymbol()))), responses);
}
}
@Service
class PriceService(
private val streams: ActorStreams,
private val ticker: SpringActorHandle<TickerActor.Command>, // spawned at startup
) : PriceServiceGrpc.PriceServiceImplBase() {
override fun watch(request: WatchRequest, responses: StreamObserver<Price>) {
ActorGrpc.respond(
streams.messages(Price::class.java).flux { ref -> ticker.tell(Subscribe(ref)) },
responses,
)
}
override fun getPrice(request: PriceRequest, responses: StreamObserver<Price>) {
ActorGrpc.respond(streams.ask(ticker.ask(GetPrice(request.symbol))), responses)
}
}
- It requests the next element only while the call is ready to send, so a slow client slows the
Flux. WithackTo, the slowness reaches the sending actor. - When the client cancels or disconnects, the
Fluxis cancelled and the reference stops. - When the
Fluxfails, the call ends with the error's gRPC status (UNKNOWNunless the error is aStatusExceptionorStatusRuntimeException).
actor-streams does not bring grpc-java: the application has it already (directly or through Spring gRPC).
The bridge is built against grpc-java 1.83 and uses only its stable grpc-stub API.
Related
- Reactor Streams:
ask,askEachandmessages - Chat example: the WebSocket bridge in a full application
- Kotlin: suspend functions and coroutine controllers