Server-sent Events (SSE) Connector

The SSE connector provides a continuous source of server-sent events recovering from connection failure.

Reported issues

Tagged issues at Github

Artifacts

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-sse" % "0.19"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-sse_2.12</artifactId>
  <version>0.19</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-sse_2.12', version: '0.19'
}

Usage

Simply define an EventSource by giving a URI, a function for sending HTTP requests and an optional initial value for Last-Evend-ID header:

Scala
val eventSource = EventSource(Uri(s"http://$host:$port"), send, Some("2"), 1.second)
Java

final Http http = Http.get(system); Function<HttpRequest, CompletionStage<HttpResponse>> send = (request) -> http.singleRequest(request, materializer); final Uri targetUri = Uri.create(String.format("http://%s:%d", host, port)); final Optional<String> lastEventId = Optional.of("2"); Source<ServerSentEvent, NotUsed> eventSource = EventSource.create(targetUri, send, lastEventId, materializer);

Then happily consume ServerSentEvents:

Scala
val events =
  eventSource.throttle(1, 500.milliseconds, 1, ThrottleMode.Shaping).take(nrOfSamples).runWith(Sink.seq)
Java

int elements = 1; FiniteDuration per = FiniteDuration.create(500, TimeUnit.MILLISECONDS); int maximumBurst = 1; eventSource .throttle(elements, per, maximumBurst, ThrottleMode.shaping()) .take(nrOfSamples) .runWith(Sink.seq(), materializer);
The source code for this page can be found here.