• This repository has been archived on 28/Oct/2021
  • Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Akka HTTP now includes Akka SSE, this project is at EOL

Akka SSE

Important: As of version 10.0.8, Akka HTTP now includes Akka SSE. Hence this project has come to an end.

Join the chat at https://gitter.im/hseeberger/akka-sse Build Status Maven Central

Akka SSE adds support for Server-Sent Events (SSE) – a lightweight and standardized technology for pushing notifications from a HTTP server to a HTTP client – to Akka HTTP. In contrast to WebSocket, which enables two-way communication, SSE only allows for one-way communication from the server to the client. If that's all you need, SSE offers advantages, because it's much simpler and relies on HTTP only.

Since version 2 Akka SSE supports both Scala and Java, even if the below examples only show Scala.

Getting Akka SSE

Akka SSE is published to Bintray and Maven Central.

// All releases including intermediate ones are published here,
// final ones are also published to Maven Central.
resolvers += Resolver.bintrayRepo("hseeberger", "maven")

libraryDependencies ++= Seq(
  "de.heikoseeberger" %% "akka-sse" % "3.0.0",
  ...
)

Usage – basics

Akka SSE models an event stream as Source[ServerSentEvent, Any] with Source from Akka Streams and ServerSentEvent from Akka SSE. ServerSentEvent is a case class with the following fields:

  • data: String – the actual payload, may span multiple lines
  • type: Option[String] with default None – optional qualifier, e.g. "added", "removed", etc.
  • id: Option[String] with default None – optional identifier
  • retry: Option[Int] with default None – optional reconnection delay in milliseconds

More informatioon about the above fields can be found in the SSE specification.

Usage – server-side

In order to respond to a HTTP request with an event stream, you have to bring the implicit ToResponseMarshaller[Source[ServerSentEvent, Any]] defined by EventStreamMarshalling into the scope defining the respective route:

object TimeServer {

  ...

  private def route = {
    import Directives._
    import EventStreamMarshalling._ // That does the trick!

    def assets = ...

    def events =
      path("events") {
        get {
          complete {
            Source
              .tick(2.seconds, 2.seconds, NotUsed)
              .map(_ => LocalTime.now())
              .map(timeToServerSentEvent)
              .keepAlive(1.second, () => ServerSentEvent.heartbeat)
          }
        }
      }

    assets ~ events
  }

  private def timeToServerSentEvent(time: LocalTime) =
    ServerSentEvent(DateTimeFormatter.ISO_LOCAL_TIME.format(time))
}

To send periodic heartbeats, simply use the keepAlive standard stage with a ServerSentEvent.heartbeat which has am empty data field and hence is ignored by clients according to the specification.

Usage – client-side

In order to unmarshal server-sent events as Source[ServerSentEvent, NotUsed], you have to bring the implicit FromEntityUnmarshaller[Source[ServerSentEvent, NotUsed]] defined by EventStreamUnmarshalling into scope:

import EventStreamUnmarshalling._ // That does the trick!
import system.dispatcher

Http()
  .singleRequest(Get("http://localhost:8000/events"))
  .flatMap(Unmarshal(_).to[Source[ServerSentEvent, NotUsed]])
  .foreach(_.runForeach(println))

References

Contribution policy

Contributions via GitHub pull requests are gladly accepted from their original author. Along with any pull requests, please state that the contribution is your original work and that you license the work to the project under the project's open source license. Whether or not you state this explicitly, by submitting any copyrighted material via pull request, email, or other means you agree to license the material under the project's open source license and warrant that you have the legal authority to do so.

License

This code is open source software licensed under the Apache 2.0 License.

More Repositories

1

akka-http-json

Integrate some of the best JSON libs in Scala with Akka HTTP
Scala
587
star
2

constructr

Coordinated (etcd, ...) cluster construction for dynamic (cloud, containers) environments
Scala
214
star
3

reactive-flows

Reactive web app with Akka and AngularJS
Scala
80
star
4

eventsourced

Event sourced entities in Rust.
Rust
28
star
5

akka-log4j

Logging backend for Akka based on Log4j
Scala
28
star
6

whirlwind-tour-akka-typed

Demo code for my Scala Exchange workshop "Whirlwind Tour of Akka Typed"
Scala
25
star
7

accessus

Access log for Akka HTTP based servers
Scala
25
star
8

demo-phantom-types

Demo of phantom types in Scala
Scala
13
star
9

lyas

Learn you Akka Streams for great Good!
Scala
10
star
10

welcome-akka-typed

Demo code for my "Farewell Any => Unit, welcome Akka Typed!" talk
Scala
10
star
11

akkluster

Demo application to visualize the status of the member nodes
Scala
9
star
12

log4dotty

Experimental (just for learning) logging library powered by Dotty / Scala 3 macros
Scala
6
star
13

demo-equality

Demos for type-safe equality with Scala implicits
Scala
6
star
14

slf4s

Scala 3 sugar for efficient logging
Scala
5
star
15

xtream

Demo code for my Akka Streams to the Extreme talk
Scala
5
star
16

game-of-life

Simple and idiomatic Scala 3 implementation of the famous Conway's Game of Life
Scala
3
star
17

deck-akka-cluster-coreos

Slide deck of Akka Cluster on CoreOS talk
JavaScript
3
star
18

bayer-akka

the Boarische Webserver with Scala and Akka
Scala
1
star
19

hseeberger.github.io

Stylus
1
star
20

bayer-cli

Rust CLI for the Boarische Webserver (bayer-warp, bayer-akka, ...)
Rust
1
star
21

hello-rs

Simple dockerized Rust/Axum based HTTP server for demo purposes.
Rust
1
star
22

scala-train

Begleitende Fallstudie zum Buch "Durchstarten mit Scala"
Scala
1
star
23

deck-reactive-apps

JavaScript
1
star
24

echo

Simple gRPC example with an Scala/Akka server and a Rust/tonic client.
Scala
1
star
25

waltz

Experimental actor framework
Rust
1
star
26

log4scala

Scala 3 sugar for logging
Scala
1
star
27

rusty-bank

Example application using eventsourced.
Rust
1
star