= Server Side Events Extension

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

The SSE (Server Side Events) module integrates with the xref:vw:ROOT:about.adoc[Wicket Viewer], and provides the ability to dynamically update properties of type xref:valuetypes:markdown:about.adoc[Markdown] and xref:valuetypes:asciidoc:about.adoc[Asciidoc].
One use case could be to render a progress bar for a long-running action.



== Setup

=== Dependency Management

In your application's top level `pom.xml`, add a dependency for this module's own bill of materials (BOM):

[source,xml,subs="attributes+"]
.pom.xml
----
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.causeway.extensions</groupId>
            <artifactId>causeway-extensions-sse</artifactId>
            <scope>import</scope>
            <type>pom</type>
            <version>{page-causewayprev}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
----

=== Dependencies / Imports

In those modules where there are domain objects to be dynamically updated using SSE, add a dependency/import to the applib module:

* add this dependency:
+
[source,xml,subs="attributes+"]
.pom.xml
----
<dependencies>
    <dependency>
        <groupId>org.apache.causeway.extensions</groupId>
        <artifactId>causeway-extensions-sse-applib</artifactId>
    </dependency>
</dependencies>
----

* and `@Import` this module:
+
[source,java]
.MyModule.java
----
@Configuration
@Import({
    CausewayModuleExtSseApplib.class,
    // ...
})
public class MyModule { ... }
----

In addition, in the webapp module of your application, add the following dependency:

[source,xml]
.pom.xml
----
<dependency>
    <groupId>org.apache.causeway.extensions</groupId>
    <artifactId>causeway-extensions-sse-wicket</artifactId>
</dependency>
----

And in your application's xref:userguide::modules.adoc#appmanifest[App Manifest], import the extension's implementation module:

[source,java]
.AppManifest.java
----
@Configuration
@Import({
        CausewayModuleExtSseWicket.class,
        ...
})
public class AppManifest {
}
----


== Usage

By way of example, let's see how to render a progress bar.
We'll start with a background task which for demo purposes wil just count slowly up to some figure.
This acts as a source of SSE events:

[source,java]
.DemoTask.java
----
@Named("demo.DemoTask")
@DomainObject(nature=Nature.VIEW_MODEL, editing=Editing.DISABLED)
@RequiredArgsConstructor(staticName="of")
public class DemoTask implements SseSource {                // <.>

    @ObjectSupport public String title() {
        return String.format("DemoTask '%s'", Integer.toHexString(hashCode()));
    }

    private final int totalSteps;
    private TaskProgress taskProgress;

    @Override
    public void run(final SseChannel eventStream) {
        taskProgress = TaskProgress.of(new LongAdder(), totalSteps);
        for(int i=0; i<totalSteps; ++i) {
            _ThreadSleep.millis(1000);
            taskProgress.getStepsProgressed().increment();
            eventStream.fire(this);                     // <.>
        }
    }

    @Override
    public String getPayload() {                        // <.>
        return taskProgress.toHtmlProgressBar();
    }
}
----
<.> Implements xref:refguide:extensions:index/sse/applib/annotations/SseSource.adoc[SseSource], meaning that it must implement `run(SseChannel)`.
<.> Periodically emits events using the provided xref:refguide:extensions:index/sse/applib/service/SseChannel.adoc[SseChannel].
<.> Also provides a payload, being a string representation of progress to date.

It's idiomatic to use HTML as the payload, so that it can be rendered in a xref:refguide:applib:index/value/Markup.adoc[Markup] property (see later).
This HTML payload is built using a helper class `TaskProgress`:

[source,java]
.TaskProgress.java
----
@Data(staticConstructor="of")
public class TaskProgress {

    private final LongAdder stepsProgressed;
    private final long totalSteps;

    public double progressedRelative() {
        final double totalReciprocal = 1. / totalSteps;
        return stepsProgressed.doubleValue() * totalReciprocal;
    }
    public double progressedPercent() {
        return Math.min(progressedRelative()*100., 100.);
    }
    public int progressedPercentAsInt() {
        return (int) Math.round(progressedPercent());
    }

    public String toHtmlProgressBar() {
        final int percent = progressedPercentAsInt();
        return stepsProgressed + "/" + totalSteps +
                "<br/>" +
                "<br/>" +
                "<div class=\"progress\">" +
                "    <div class=\"progress-bar\" " +
                         "role=\"progressbar\" " +
                         "style=\"width: " + percent + "%\" " +
                         "aria-valuenow=\""+percent+"\" " +
                         "aria-valuemin=\"0\" " +
                         "aria-valuemax=\"100\">" +
                    "</div>" +
                "</div>";
    }
}
----

As mentioned above, we will use  a xref:refguide:applib:index/value/Markup.adoc[Markup] property to display the render the progress:

[source,java]
----
@XmlElement @XmlJavaTypeAdapter(Markup.JaxbToStringAdapter.class)
@Property
@ServerSentEvents(observe=DemoTask.class)           // <.>
@Getter @Setter Markup progressView;
----
<.> observes the state of this background task.
+
IMPORTANT: If more than one instance of a task (of this class) runs at the same time, there is (currently) no way to distinguish their events.

Finally, the task is kicked off using the xref:refguide:extensions:index/sse/applib/service/SseService.adoc[].
This runs in the background using a separate thread:

[source,java]
----
@Action
public SseDemoPage startSimpleTask() {
    final DemoTask demoTask = DemoTask.of(100);
    sseService.submit(demoTask, ExecutionBehavior.REQUIRES_NEW_SESSION);  // <.>
    return this;
}

@Inject SseService sseService;
----
<.> The `ExecutionBehaviour` determines whether the background thread should run in the context of a full-blown xref:refguide:applib:index/services/iactn/Interaction.adoc[].
+
Most of the time this will be what you want, but if the background thread runs outside of the framework (eg perhaps just calls out to a web service), then instead `Execution.SIMPLE` can be used.

