Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you send data from Kafka streams to clients via Websockets in PlayFramework?

I am using the Playframework along with Scala. I am trying to consume data from Kafka, process the data, and then push the data via websockets to the client. Unfortunately, I am still relatively a newbie with this technology.

When going over the documentation, they mentioned to create an actor when you want to create websockets. Code below is from the website. web socket

import play.api.libs.json.JsValue
import play.api.mvc._
import play.api.libs.streams._

class Controller4 @Inject() (implicit system: ActorSystem, materializer: Materializer) {
  import akka.actor._

  class MyWebSocketActor(out: ActorRef) extends Actor {
     import play.api.libs.json.JsValue
     def receive = {
       case msg: JsValue =>
        out ! msg
     }

     // do i include my kafka consumer here???

  }

  object MyWebSocketActor {
    def props(out: ActorRef) = Props(new MyWebSocketActor(out))
  }

  def socket = WebSocket.accept[JsValue, JsValue] { request =>
    ActorFlow.actorRef(out => MyWebSocketActor.props(out))
  }
}

My question is, where do I put the Kafka consumer code. Do I place it inside the actor? Is this best practice? The reason I am concerned about putting it the kafka consumer there is that it will block.

Thank you in advance for your help.

like image 837
dcart1234 Avatar asked Oct 15 '25 17:10

dcart1234


1 Answers

Akka Streams + Reactive Kafka actually makes this pretty easy. Just wire a Kafka Source to the WebSocket Source to send Kafka messages to a client via the WebSocket. Here is a complete demo with code: https://www.jamesward.com/2016/05/25/combining-reactive-streams-heroku-kafka-and-play-framework/

like image 128
James Ward Avatar answered Oct 18 '25 07:10

James Ward