Alpakka sample
Read from a Kafka topic and push the data to connected websocket clients
Clients may connect via websockets and will receive data read from a Kafka topic. The websockets are implemented in Akka HTTP and Alpakka Kafka subscribes to the Kafka topic.
Browse the sources at Github { open=new }.
To try out this project clone the Alpakka Samples repository { open=new } and find it in the alpakka-sample-kafka-to-websocket-clients
directory.
Running
The sample spawns a test Kafka server with docker.
sbt "runMain samples.javadsl.Main"
You can connect to ws://127.0.0.1/events
to receive messages over websockets. E.g. Using websocat
as a simple WS client.
To listen to events coming in on the websocket use websocat
to connect to the /events
endpoint.
websocat -v ws://127.0.0.1:8081/events
You can use curl
to post messages to the topic.
curl http://127.0.0.1:8081/push?value=message
Dependencies
- Dependencies (sbt notation)
-
source
val AkkaVersion = "2.7.0" val AkkaHttpVersion = "10.4.0" val AlpakkaKafkaVersion = "4.0.0" val AkkaDiagnosticsVersion = "2.0.0-M4" "com.typesafe.akka" %% "akka-stream" % AkkaVersion, "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion, "com.typesafe.akka" %% "akka-stream-kafka" % AlpakkaKafkaVersion, "com.lightbend.akka" %% "akka-diagnostics" % AkkaDiagnosticsVersion, // Logging "com.typesafe.akka" %% "akka-slf4j" % AkkaVersion, "ch.qos.logback" % "logback-classic" % "1.2.3",
All Alpakka samples
Show Alpakka samples listing.