PHPackages                             vadimon/kafka-router-for-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. vadimon/kafka-router-for-laravel

ActiveLibrary[API Development](/categories/api)

vadimon/kafka-router-for-laravel
================================

Package for Laravel to route consumed kafka messages to controllers same way as HTTP requests

1.0.1(3y ago)212MITPHPPHP ^7.3|^8

Since Feb 4Pushed 2y ago1 watchersCompare

[ Source](https://github.com/vadimonus/kafka-router-for-laravel)[ Packagist](https://packagist.org/packages/vadimon/kafka-router-for-laravel)[ Docs](https://github.com/vadimonus/kafka-router-for-laravel)[ RSS](/packages/vadimon-kafka-router-for-laravel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

Laravel router for Kafka messages
=================================

[](#laravel-router-for-kafka-messages)

Introduction
------------

[](#introduction)

This package provides a way to route Kafka messages to controllers same way as for web routes.

Installation
------------

[](#installation)

Require this package with composer.

```
composer require vadimon/kafka-router-for-laravel
```

Install application service provider and route map file.

```
php artisan vendor:publish --provider="Vadimon\\Laravel\\Kafka\\Router\\KafkaRouterPackageServiceProvider"
```

Register `KafkaRouteServiceProvider` in `config/app.php`

```
'providers' => [
    // Other Service Providers

    App\Providers\KafkaRouterServiceProvider::class,
],
```

Usage
-----

[](#usage)

### Consume

[](#consume)

This package is aimed only to route messages. You need some other solution to consume message.

### Route

[](#route)

Simply pass consumed message to `KafkaRoute::dispatch`, and it will be routed to controller, specified by route map.

```
KafkaRoute::dispatch($message);
```

If you consume messages from multiple connections, you can also provide connection name.

```
KafkaRoute::dispatch($message, $connectionName);
```

### Route map

[](#route-map)

By default, route map is defined in `routes/kafka.php`. You can override this in `KafkaRouterServiceProvider::map()`method.

#### Topics

[](#topics)

The handler for messages is defined by `KafkaRoute::topic` method.

```
KafkaRoute::topic('FirstTopic', 'KafkaController@handleFirst']);
KafkaRoute::topic('SecondTopic', [KafkaController::class, 'handleSecond']);
KafkaRoute::topic('ThirdTopic', InvokableController::class);
KafkaRoute::topic('FourthTopic', function (\RdKafka\Message $message) {
    // handle message
});
KafkaRoute::topic('*', function () {
});
```

Topic names are case-sensitive. `*` mean any topic.

#### Fallback route

[](#fallback-route)

Routes are matched in order, in which they are provided. If you need some fallback, put handler for `*` after all other routes

```
// All other routes
KafkaRoute::topic('*', [KafkaController::class, 'fallbackHandler']);
```

#### Connections

[](#connections)

You can use `KafkaRoute::connection` to specify different handlers for different connections.

```
KafkaRoute::connection('FirstConnection', base_path('routes/kafka/first.php'));
KafkaRoute::connection('SecondConnection', function () {
    KafkaRoute::topic('FirstTopic', 'KafkaController@handleFirstTopicOfSecondConnection']);
    KafkaRoute::topic('*', 'KafkaController@handleAnyTopicOfSecondConnection']);
});
KafkaRoute::topic('ThirdTopic', 'KafkaController@handleThirdTopicOfAnyConnection']);
```

Connection names are case-sensitive. `*` mean any connection.

#### Middleware

[](#middleware)

You can assign middleware in route table

```
// Middleware, defined as class
KafkaRoute::middleware(KaffkaMiddleware::class, function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});
// Middleware, defined as closure
$middleware = function (\RdKafka\Message $message, \Closure $next) {
    // some action
    $next($message);
}
KafkaRoute::middleware($middleware, function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});
// Multiple middlewares at once
KafkaRoute::middleware([Middleware1::class, Middleware2::class], function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});
```

#### Groups

[](#groups)

Groups allow to specify `connection` and `middleware` in one call.

```
KafkaRoute::group(['connection'=> 'SecondConnection', 'middleware' => KafkaMiddleware::class], function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});
```

### Controller

[](#controller)

You can write controllers same way, you write them for HTTP requests. The only difference - you do not need to return anything from controller.

To access message, connection name and router in controller method, you can use Dependency Injection or `KafkaRoute` facade.

```
