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:

  1. Inject NotificationPublisher<T> in the workflow constructor
  2. Call publish(Object) to send notifications during workflow steps
  3. 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 Classes
    Modifier and Type
    Interface
    Description
    static interface 
    A helper interface allowing a type safe subscription to a notification stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    publish(T msg)
    Publishes a single notification message to all subscribers.
    default akka.stream.javadsl.Sink<T,CompletionStage<akka.Done>>
    Returns an Akka Streams Sink that publishes each element it receives as a notification.
    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.
    Returns a NotificationPublisher.NotificationStream handle that can be exposed via a Workflow method for external clients to subscribe to notifications.
  • Method Details

    • publish

      void publish(T msg)
      Publishes a single notification message to all subscribers.
      Parameters:
      msg - the notification message to publish
    • publishSink

      default akka.stream.javadsl.Sink<T,CompletionStage<akka.Done>> publishSink()
      Returns an Akka Streams Sink that 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 publish
      groupingMaxNumber - maximum number of tokens to batch together
      groupingDuration - maximum time to wait before publishing a batch
      partialMapping - function to convert batched token strings into notification messages
      materializer - the Akka Streams materializer
      Returns:
      the complete concatenated string of all tokens
    • stream

      Returns a NotificationPublisher.NotificationStream handle 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