Skip to content

Kafka Sample Application

Build a consumer app that reads from a Kafka topic and writes every message to a file. You use Winter Boot for the application runtime and the Winter Kafka module from Winter Modules for topic access. You test the whole flow locally with a single-broker Kafka running in Docker.

You need PHP 8.5 or later with the swoole, rdkafka, and pcntl extensions. You also need Docker to run Kafka.

Terminal window
pecl install swoole
pecl install rdkafka

Start a single-broker Kafka before you run the app:

Terminal window
docker run -d -p 9092:9092 --name kafka \
apache/kafka:latest

The sample uses this layout:

kafka/
├── bin/
│ └── application.php # Application entry point
├── config/
│ ├── application.yml # Winter Boot application config
│ └── kafka-config.yml # Kafka bootstrap and consumer config
├── src/
│ ├── KafkaSampleApplication.php # Main application class
│ └── consumer/
│ └── MessageFileWriterConsumer.php # Kafka consumer worker
└── composer.json # Dependencies

Require the framework and the modules package:

Terminal window
composer require suvera/winter-boot suvera/winter-modules

Switch between the four source files. Each tab shows the exact file from the sample.

The single entry point. It points Winter Boot at the config directory and at the sample namespace.

<?php
namespace dev\example;
use dev\winterframework\stereotype\WinterBootApplication;
#[WinterBootApplication(
configDirectory: [__DIR__ . "/../config"],
scanNamespaces: [
['dev\\example', __DIR__ . '']
]
)]
class KafkaSampleApplication {
public static function main(): void {
$winterApp = new \dev\winterframework\core\app\WinterWebSwooleApplication();
$winterApp->run(self::class);
}
}

Switch between the two config files. application.yml enables the module, and kafka-config.yml defines the bootstrap server and consumer group.

The full sample file registers KafkaModule and sets the server and app identity:

server:
port: 8080
address: 0.0.0.0
context-path: /
winter:
application:
name: Kafka Sample Application
id: kafka-sample-app
version: 1.0.0
modules:
- module: dev\winterframework\kafka\KafkaModule
enabled: true
configFile: kafka-config.yml

See Configuration for every application.yml key.

Create the topic, start the app, send a message, then check the output file.

1. Create the topic:

Terminal window
docker exec kafka /opt/kafka/bin/kafka-topics.sh --create \
--topic my-topic \
--bootstrap-server localhost:9092

2. Start the application:

Terminal window
composer install
php bin/application.php

The Kafka worker starts polling my-topic as soon as the app boots.

3. Send a message:

Terminal window
echo "Hello Kafka" | docker exec -i kafka /opt/kafka/bin/kafka-console-producer.sh \
--topic my-topic \
--bootstrap-server localhost:9092

4. Verify consumption:

Terminal window
cat /tmp/kafka-messages.txt

You see one line per consumed message, for example:

[2026-09-10 12:00:01] Topic: my-topic | Group: my-group | Partition: 0 | Offset: 0 | Value: Hello Kafka
  • Read the Kafka module for producer APIs (KafkaService), consumer tuning, and rdkafka settings.
  • Browse all Libraries when you need SQS, S3, Redis, or Doctrine in the same app.