Last active
January 6, 2020 19:07
-
-
Save colinbes/d551f81be9412dc85c064a293f28be89 to your computer and use it in GitHub Desktop.
SSE Streaming Actor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.besterdesigns.actors | |
import java.util.Calendar | |
import _root_.akka.actor.{Actor, Props} | |
import _root_.akka.stream.scaladsl.SourceQueueWithComplete | |
import com.besterdesigns.models.Json4sFormat | |
import scala.concurrent.duration._ | |
case object StartClock | |
object StreamingEventSourceActor { | |
def name = "StreamingEventSourceActor" | |
def props(source: SourceQueueWithComplete[String]): Props = Props(new StreamingEventSourceActor(source)) | |
case class UpdateDashboard(data: String) | |
} | |
class StreamingEventSourceActor(source: SourceQueueWithComplete[String]) | |
extends Actor | |
with Json4sFormat { | |
implicit val ec = context.system.dispatcher | |
override def receive: Receive = { | |
case StartClock => { | |
//send periodic date/time to all browsers | |
val dt = Calendar.getInstance().getTime() | |
val response = s"""{"msg":"${dt}"}""" | |
context.system.scheduler.scheduleOnce(1.second) { | |
self ! StartClock | |
} | |
source.offer(response) | |
} | |
case msg: StreamingEventSourceActor.UpdateDashboard => { | |
// send broadcase to all connect browsers. Triggered by PUT method in Rest end point PUT events | |
val testMessage = | |
"""{"msg":"First Message"} | |
|""".stripMargin | |
source.offer(testMessage) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment