Working with Kafka in Swift (iOS/macOS): Libraries, Clients, and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Kafka in Swift (iOS/macOS): Libraries, Clients, and Examples



Apache Kafka is a distributed data streaming platform widely used for building event-driven architectures, log collection, analytics processing, and working with microservices. In the Swift ecosystem (iOS/macOS), Kafka is not a native technology, but there are several mature libraries that allow integrating Swift applications with Kafka brokers. In this article, we will review the main tools, their installation, and write a simple producer and consumer in Swift.



Why is Kafka needed in a Swift application?

Although Kafka is most often associated with server-side backends, mobile and desktop Swift applications can use Kafka for:

  • Receiving real-time push notifications via WebSocket + Kafka;
  • Sending user behavior events to an analytics server;
  • Real-time data synchronization between multiple devices;
  • Client-side logging with subsequent sending to a centralized system.


Main libraries for working with Kafka in Swift

Currently, there are three main approaches to integrating Kafka into Swift projects:



1. Confluent Swift Client (recommended)

Confluent Swift Client is an official wrapper over librdkafka from Confluent. The library supports a full set of features: producers, consumers, topic administration, SSL, SASL authentication. It works on iOS and macOS.

Installation via Swift Package Manager:

// Package.swiftdependencies: [    .package(url: "https://github.com/confluentinc/confluent-kafka-swift.git", from: "0.1.0")]


Producer example in Swift

Let's create a simple producer that sends a string to the test-topic topic:

import ConfluentKafka

let config = KafkaConfig()config["bootstrap.servers"] = "localhost:9092"config["client.id"] = "swift-producer"

let producer = try KafkaProducer(config: config)

let message = "Hello from Swift!"let record = KafkaProducerRecord(topic: "test-topic", value: message.data(using: .utf8)!)

try producer.send(record: record) { error, deliveryReport in if let error = error { print("Send error: \\(error)") } else { print("Message sent to partition \\(deliveryReport?.partition ?? -1)") }}

producer.flush()


Consumer example in Swift

import ConfluentKafka

let config = KafkaConfig()config["bootstrap.servers"] = "localhost:9092"config["group.id"] = "swift-consumer-group"config["auto.offset.reset"] = "earliest"

let consumer = try KafkaConsumer(config: config)try consumer.subscribe(topics: ["test-topic"])

while true { let records = try consumer.poll(timeout: 1.0) for record in records { if let value = record.value { let message = String(data: value, encoding: .utf8) ?? "failed to decode" print("Received: \\(message)") } }}


2. KafkaSwift (lightweight alternative)

KafkaSwift is a minimalistic library written in pure Swift without dependencies on C libraries. It is suitable for simple scenarios but does not support advanced features (SSL, SASL, partitioning).

Installation: via CocoaPods: pod 'KafkaSwift'.

Send example:

import KafkaSwift

let kafka = try Kafka(host: "localhost", port: 9092)try kafka.send(message: "Test message", topic: "test-topic")


3. Perfect-Kafka (for server-side Swift)

Perfect-Kafka is part of the Perfect framework, focused on server-side Swift applications (macOS, Linux). It is

Recommendations