Package rife.workflow

Class SseWorkflowBridge

java.lang.Object
rife.workflow.SseWorkflowBridge
All Implemented Interfaces:
EventListener

public class SseWorkflowBridge extends Object implements EventListener
Bridges the events of a Workflow to a server-sent events (SSE) SseBroadcaster, so that every event that is triggered or informed about in the workflow is pushed to all the connected SSE clients.

The bridge is registered like any other workflow listener:

workflow.addListener(new SseWorkflowBridge(broadcaster));

By default, a workflow event is converted with the string representation of its type as the SSE event name, and with the string representation of its data as the SSE event data. You can provide a custom converter instead, which also makes it possible to render templates as event data, or to filter events out by returning null:

workflow.addListener(new SseWorkflowBridge(broadcaster, event -> {
     if (event.getType() != MyTypes.PROGRESS) return null;
     var t = TemplateFactory.HTML.get("progress_fragment");
     t.setValueEncoded("step", event.getData());
     return new ServerSentEvent().name("progress").template(t);
 }));

The converter runs on the thread that triggers the workflow event, and the exceptions that it throws propagate to the caller of the trigger, while the paused work is still resumed.

Since:
1.10
See Also:
API Note:
The workflow engine is in a BETA STAGE and might still change.
  • Constructor Details

    • SseWorkflowBridge

      public SseWorkflowBridge(SseBroadcaster broadcaster)
      Creates a new bridge that converts workflow events with the default conversion: the string representation of the event type becomes the SSE event name, and the string representation of the event data, when it's present, becomes the SSE event data.
      Parameters:
      broadcaster - the broadcaster the converted events will be sent to
      Since:
      1.10
      See Also:
    • SseWorkflowBridge

      public SseWorkflowBridge(SseBroadcaster broadcaster, Function<Event,ServerSentEvent> converter)
      Creates a new bridge with a custom event converter.

      The converter receives every workflow event and returns the server-sent event that will be broadcast, or null when nothing should be broadcast for that workflow event.

      Parameters:
      broadcaster - the broadcaster the converted events will be sent to
      converter - the converter that will be used to convert workflow events into server-sent events
      Since:
      1.10
      See Also:
  • Method Details

    • json

      public static SseWorkflowBridge json(SseBroadcaster broadcaster)
      Creates a new bridge whose conversion transmits JSON event data, which is suitable for clients that parse the events with JSON.parse(event.data).

      The string representation of the workflow event type becomes the SSE event name, just like it does with the default conversion, while the event data, when it's present, is converted with ServerSentEvent.json(java.lang.Object).

      Parameters:
      broadcaster - the broadcaster the converted events will be sent to
      Returns:
      the new bridge instance
      Since:
      1.10
      See Also:
    • eventTriggered

      public void eventTriggered(Event event)
      Description copied from interface: EventListener
      Called when an event was triggered.
      Specified by:
      eventTriggered in interface EventListener
      Parameters:
      event - the event that was triggered