xkafka

xkafka is a functional Kafka client for Scala 3, Cats Effect, and FS2. It provides the same producer and consumer API on the JVM, Scala.js, and Scala Native while using the established Kafka client for each runtime.

Platform Kafka client Runtime
JVM Apache Kafka Java client Java 17 or newer
Scala.js librdkafka Node.js 24
Scala Native librdkafka Scala Native 0.5 or newer

The API uses Resource for producer and consumer lifecycles, Stream for consumer records, effectful serializers and deserializers, and explicit offset commits. On Scala.js it runs on Node.js, not in browsers.

xkafka also runs Kafka Streams programs, alongside Java Kafka Streams instances. It writes and reads Avro, JSON Schema and Protobuf with schemas kept in a schema registry.

To add xkafka to your build and see what each platform needs, start with Getting started.

// Write one record to the topic "events", and wait until it is delivered
import cats.data.NonEmptyList
import cats.effect.{IO, IOApp}

import xkafka.*

object ProduceExample extends IOApp.Simple:
  private val utf8 = Serializer.utf8[IO]

  private val settings =
    ClientSettings.from(NonEmptyList.one("localhost:9092")).andThen: client =>
      ProducerSettings.from(client, utf8, utf8)

  override def run: IO[Unit] =
    for
      topic    <- Topic.from("events").liftTo[IO]
      producer <- settings.liftTo[IO]  // Raise invalid settings as a failure
      // Acquire a producer, send the record, and release the producer
      _        <- KafkaClient[IO].producer(producer).use(_.produceAndAwait(NonEmptyList.one(ProducerRecord(topic, "key", "value"))))
    yield ()