Skip to content

Instantly share code, notes, and snippets.

@ryanmiville
Last active March 1, 2018 00:15
Show Gist options
  • Save ryanmiville/615c850c37bffc7f0c711520f8832a9a to your computer and use it in GitHub Desktop.
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
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