PHPackages                             smskin/laravel-service-bus - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. smskin/laravel-service-bus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

smskin/laravel-service-bus
==========================

Service bus for laravel

2.0.6(3y ago)01.7k3MITPHPPHP ^8.1

Since Jul 20Pushed 3y ago2 watchersCompare

[ Source](https://github.com/smskin/laravel-service-bus)[ Packagist](https://packagist.org/packages/smskin/laravel-service-bus)[ Docs](https://github.com/smskin/laravel-service-bus)[ RSS](/packages/smskin-laravel-service-bus/feed)WikiDiscussions main Synced 3d ago

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

Laravel service bus
-------------------

[](#laravel-service-bus)

This library provides communication between multiple services. Supports sync and async communications.

This library uses:

-  - Library for supports AMPQ. Some class was overrides for support current library logic.
-  - code base of supervisor part

### Base logic

[](#base-logic)

Base logic builds on package structure. This structure very similar to .Net MassTransit packages. Package contains:

- package meta
- message type
- message

Algorithm:

- Sender creates package with message
- Sender submit package to consumer
- Consumer defines requested package by message type and deserialize package
- Consumer executes logic with received package

#### Synchronous communications

[](#synchronous-communications)

- Sender creates package with message
- Sender submit package to consumer with synchronous method
    - Library serialized package to json
    - Library submits http request to consumer host
    - Consumer process some logic with received package
    - Consumer returns new package with response
- Sender receives response package from consumer

#### Asynchronous communications

[](#asynchronous-communications)

Asynchronous communication provides by RabbitMQ

- Sender creates package with message
- Sender submit package to consumer with asynchronous method
    - Library serialized package to json
    - Library submits package to the RabbitMQ exchange
    - RabbitMQ submits received package to queue
- Consumer listens RabbitMQ queue
- Consumer receives package from queue
- Consumer process some logic with received package

### Installation

[](#installation)

- `composer require smskin/laravel-service-bus`
- `php artisan vendor:publish --provider="SMSkin\ServiceBus\Providers\ServiceProvider"`
- Configure RabbitMQ connection in `service-bus.php` config file
- Run `php artisan service-bus:setup` from initialize RabbitMQ exchanges and queues
- Add service bus consumer command to the supervisor conf

Supervisor config to provides service bus consumer

```
[program:laravel-service-bus]
process_name=%(program_name)s
command=php /var/www/html/artisan service-bus:supervisor
autostart=true
autorestart=true
user=www-data
group=www-data
redirect_stderr=true
numprocs=1
stderr_logfile=/dev/stderr
stdout_logfile=/dev/stdout

```

### Artisan commands

[](#artisan-commands)

- `service-bus:setup` - command for initialize RabbitMQ exchanges and queues
- `service-bus:supervisor` - service bus supervisor. Start processes for consume packages from RabbitMQ queues
- `service-bus:delete-all` - command deletes exchanges and queues from RabbitMQ
- `service-bus:list` - command lists exchanges and queues
- `service-bus:publish` - command for submit test message to RabbitMQ exchange (for tests)
- `service-bus:consume` - consumer command for receive packages from RabbitMQ queue (uses by supervisor command)

### Service bus supervisor

[](#service-bus-supervisor)

Supervisor command run consumer process for each registered consumer (Config file `service-bus.php`, section `supervisor`)

### Overriding default enums

[](#overriding-default-enums)

You can create new enum and override calling it in `service-bus.php` config file. It provides extend the default consumers and exchanges.

For example

- Create Exchanges enum
- Extends it from base SMSkin\\ServiceBus\\Enums\\Exchanges
- Change the `items` method for provide new exchanges
- Replace default enum in `service-bus.php` config file (line 14)

### Classes

[](#classes)

- Package - base exchange data package
- Message - package payload (DTO)
- Processor - class, that running on receive incoming package. Can return new Package in synchronous exchange mode.

#### Exchanges

[](#exchanges)

Register exchanges in Exchanges enum (SMSkin\\ServiceBus\\Enums\\Exchanges - can be overrides)

- id - internal id
- connection - id of connection (SMSkin\\ServiceBus\\Enums\\Connections)
- rabbitMqName - RabbitMQ name of exchange
- attributes - RabbitMQ exchange attributes
    - exchangeType (exchange\_type)
    - passive
    - durable
    - autoDelete (auto\_delete)
    - internal
    - nowait
    - throwExceptionOnRedeclare (throw\_exception\_on\_redeclare)
    - throwExceptionOnBindFail (throw\_exception\_on\_bind\_fail)

#### Queues

[](#queues)

Register queues in Queues enum (SMSkin\\ServiceBus\\Enums\\Queues - can be overrides)

- id - internal id
- connection - id of connection (SMSkin\\ServiceBus\\Enums\\Connections)
- rabbitMqName - RabbitMQ name of queue
- attributes
    - passive
    - durable
    - autoDelete (auto\_delete)
    - internal
    - nowait
    - exclusive
    - bind - array of binds
        - exchange - id of exchange (SMSkin\\ServiceBus\\Enums\\Exchanges)
        - routing\_key
    - arguments
        - x-max-priority - maximal message priority

#### Publishers

[](#publishers)

Register publishers in Publishers enum (SMSkin\\ServiceBus\\Enums\\Publishers - can be overrides)

- id - internal id
- exchange - id of exchange (SMSkin\\ServiceBus\\Enums\\Exchanges)

#### Consumers

[](#consumers)

Register consumers in Consumers enum (SMSkin\\ServiceBus\\Enums\\Consumers - can be overrides)

- id - internal id
- queue - id of queue (SMSkin\\ServiceBus\\Enums\\Queues)
- prefetchCount

#### Hosts

[](#hosts)

Register hosts in Hosts enum (SMSkin\\ServiceBus\\Enums\\Hosts - can be overrides)

- id - internal id
- host - url of consumer host (for provide synchronous service bus)

### Exchange package

[](#exchange-package)

Package must provide links to processor and message classes. Package register in Packages enum ( SMSkin\\ServiceBus\\Enums\\Packages - can be overrides). Exchange packages has very simple structure, that support reproduce this logic in any languages.

Example of serialized package:

```
{
    "messageId": "e5e471ae-62e3-46c5-93fd-6dd589c32748",
    "correlationId": "2712f617-4d55-4851-ad2c-5c841224e251",
    "conversationId": null,
    "initiatorId": null,
    "sourceAddress": null,
    "destinationAddress": null,
    "messageType": [
        "urn:message:TEST_ASYNC_LOCAL"
    ],
    "message": {
        "name": "Sergey"
    },
    "sentTime": "2022-06-07T10:19:15.152620Z",
    "host": null
}
```

Example of package:

```
