Last active
March 1, 2018 00:15
-
-
Save ryanmiville/615c850c37bffc7f0c711520f8832a9a to your computer and use it in GitHub Desktop.
A simple example of responding to a websocket message with backpressure using Akka
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
class WsPingPong extends Actor { | |
import WsPingPong._ | |
private implicit val materializer = ActorMaterializer() | |
private val queue = | |
Source.queue[Message](Int.MaxValue, OverflowStrategy.backpressure) | |
.via(Http().webSocketClientFlow(WebSocketRequest("wss://pingpongsockets.org"))) | |
.to(Sink.actorRefWithAck(self, Init, Ack, Complete)) | |
.run() | |
override def receive: Receive = { | |
case Init => | |
sender ! Ack | |
case TextMessage.Strict("ping") => | |
queue.offer(TextMessage("pong")) | |
sender ! Ack | |
case TextMessage.Strict("pong") => | |
queue.offer(TextMessage("ping")) | |
sender ! Ack | |
case Complete => | |
context.stop(self) | |
} | |
} | |
object WsPingPong { | |
private case object Init | |
private case object Ack | |
private case object Complete | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment