- How do I publish a message to a Kafka topic in Laravel using this package?
- Use the `Kafka::publish()` method with a topic name and payload. For example, `Kafka::publish('orders.created', ['order_id' => 123])` sends a JSON message. The package handles serialization automatically and supports raw messages or structured headers for advanced use cases.
- Does this package support Kafka consumers in Laravel, and how do I set them up?
- Yes, the package provides a `KafkaConsumer` facade for consuming messages. You can use it in a Laravel job or command with `KafkaConsumer::consume('topic-name', function ($message) { ... })`. For long-running consumers, integrate with Laravel Horizon or use polling with the `blocking` option.
- Which Laravel versions are compatible with this package?
- The package supports Laravel 9.x and 10.x. Check the [GitHub repository](https://github.com/mateusjunges/laravel-kafka) for the latest version compatibility. The documentation also includes version-specific installation instructions.
- Can I test Kafka interactions in Laravel without a real broker?
- Yes, the package includes a fake testing helper: `Kafka::fake()` lets you assert published messages or consumed messages in unit/integration tests. It works seamlessly with Laravel’s testing tools like PHPUnit or Pest, reducing flakiness in CI environments.
- How does this package handle Avro or Protobuf schemas for Kafka messages?
- The package supports schema-based messages by separating headers and bodies. For Avro/Protobuf, integrate with Kafka Connect or libraries like `rdkafka-php` extensions. The package doesn’t enforce schema validation but provides the flexibility to add it via middleware or custom logic.
- What’s the best way to handle errors when producing or consuming Kafka messages?
- The package includes retry logic for failed produces. For consumers, you can configure dead-letter queues or manual offset commits. Use the `Kafka::producer()->setRetry()` method or handle exceptions in your consumer callbacks to implement custom error strategies.
- Can I use this package with managed Kafka services like Confluent Cloud or AWS MSK?
- Yes, the package abstracts broker configuration via `.env` or `config/kafka.php`. Point the `brokers` setting to your managed service’s endpoints (e.g., `PLAINTEXT://your-broker:9092`). Schema registries (e.g., Confluent Schema Registry) can be integrated via custom middleware or Kafka Connect.
- How do I scale Kafka consumers in Laravel for parallel processing?
- Use Kafka consumer groups to distribute messages across multiple Laravel workers. Configure the `group.id` in your consumer setup and deploy multiple instances of your consumer job or command. The package supports rebalancing out of the box, but test with tools like `Kafka::fake()` to simulate group changes.
- Is there a performance impact when using this package compared to raw Kafka clients?
- The package adds minimal overhead for serialization and Laravel integration but relies on `rdkafka` for performance-critical operations. For high-throughput topics, benchmark your use case and consider batching messages or enabling compression in the Kafka producer configuration.
- What alternatives exist for Kafka in Laravel, and why choose this package?
- Alternatives include `php-kafka` (pure PHP) or `confluent-php-client`. This package stands out for its Laravel-native API, built-in testing tools (`Kafka::fake()`), and seamless integration with Laravel’s service container and queues. It’s ideal if you want a developer-friendly, event-driven solution without sacrificing flexibility.