1. Go to this page and download the library: Download limlabs/kafka-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
limlabs / kafka-bundle example snippets
namespace App\Controller;
use LimLabs\KafkaBundle\Factory\KafkaFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class ExampleController extends AbstractController
{
public function __construct(
private readonly KafkaFactory $kafkaFactory
) {
}
#[Route('/example', name: 'app_example')]
public function index(): JsonResponse
{
$client = $this->kafkaFactory->getKafkaClient();
$producer = $client->getProducer();
$producer->createTopic('test')->produce('TestMessage');
$producer->flush();
[...]
}
namespace App\Consumers;
use LimLabs\KafkaBundle\Enum\ConsumerResponse;
use LimLabs\KafkaBundle\Kafka\Consumer\ConsumerConfiguration;
use LimLabs\KafkaBundle\Kafka\Consumer\KafkaConsumer;
class ExampleQueueConsumer implements KafkaConsumer
{
public function consume(string $message): ConsumerResponse
{
var_dump($message);
return ConsumerResponse::SUCCESS;
}
public function getConsumerConfiguration(): ConsumerConfiguration
{
return ConsumerConfiguration::createConfiguration([
'consumer_group' => 'example_consumer_group',
'connection' => 'different_client', #if you do not set this 'default' will be used
'subscribed_topics' => [
'example_topic'
]
]);
}
}
public function getConsumerConfiguration(): ConsumerConfiguration
{
$configuration = new ConsumerConfiguration();
$configuration->setConnection('different_client');
$configuration->setConsumerGroup('example_consumer_group');
$configuration->setSubscribedTopics(['example_topic']);
return $configuration;
}