Package akka.javasdk
Interface NotificationPublisher<T>
- Type Parameters:
T- the type of notification messages
@ApiMayChange
public interface NotificationPublisher<T>
A publisher for sending notifications to external subscribers. Notifications can be used to
stream progress updates, status changes, or any other messages to clients. Currently, only
supported in the Workflow component.
To use notifications in a workflow:
- Inject
NotificationPublisher<T>in the workflow constructor - Call
publish(Object)to send notifications during workflow steps - Expose a method returning
stream()for clients to subscribe
Example of subscribing to notifications from a client:
componentClient.forWorkflow(workflowId)
.notificationStream(MyWorkflow::updates)
.source()
.runForeach(notification -> System.out.println("Received: " + notification), materializer);
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA helper interface allowing a type safe subscription to a notification stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidPublishes a single notification message to all subscribers.default akka.stream.javadsl.Sink<T, CompletionStage<akka.Done>> Returns an Akka StreamsSinkthat publishes each element it receives as a notification.default StringpublishTokenStream(akka.stream.javadsl.Source<String, akka.NotUsed> tokenSource, int groupingMaxNumber, Duration groupingDuration, akka.japi.function.Function<String, T> partialMapping, akka.stream.Materializer materializer) Publishes a stream of string tokens (typically from an LLM response) as batched notifications.stream()Returns aNotificationPublisher.NotificationStreamhandle that can be exposed via a Workflow method for external clients to subscribe to notifications.
-
Method Details
-
publish
Publishes a single notification message to all subscribers.- Parameters:
msg- the notification message to publish
-
publishSink
Returns an Akka StreamsSinkthat publishes each element it receives as a notification. Useful for integrating with stream-based source of notification.- Returns:
- a sink that publishes notifications
-
publishTokenStream
default String publishTokenStream(akka.stream.javadsl.Source<String, akka.NotUsed> tokenSource, int groupingMaxNumber, Duration groupingDuration, akka.japi.function.Function<String, T> partialMapping, akka.stream.Materializer materializer) Publishes a stream of string tokens (typically from an LLM response) as batched notifications. Tokens are grouped by count and time window, then published as notifications using the provided mapping function. This is useful for streaming AI/LLM responses to clients in real-time.Example usage for streaming LLM tokens:
Source<String, NotUsed> tokenSource = componentClient.forAgent() .tokenStream(MyAgent::generate) .source(request); notificationPublisher.publish(new MyNotification.ResponseStart()); String fullResponse = notificationPublisher.publishTokenStream( tokenSource, 10, // batch up to 10 tokens Duration.ofMillis(200), // or every 200ms MyNotification.ResponseDelta::new, // wrap batched text in notification materializer); notificationPublisher.publish(new MyNotification.ResponseEnd());- Parameters:
tokenSource- the source of string tokens to publishgroupingMaxNumber- maximum number of tokens to batch togethergroupingDuration- maximum time to wait before publishing a batchpartialMapping- function to convert batched token strings into notification messagesmaterializer- the Akka Streams materializer- Returns:
- the complete concatenated string of all tokens
-
stream
Returns aNotificationPublisher.NotificationStreamhandle that can be exposed via a Workflow method for external clients to subscribe to notifications.Example:
public NotificationStream<MyNotification> updates() { return notificationPublisher.stream(); }- Returns:
- a notification stream handle for client subscription
-