{"id":22447007,"url":"https://github.com/needle-project/laravel-rabbitmq","last_synced_at":"2025-07-02T01:32:56.515Z","repository":{"id":44996940,"uuid":"133374841","full_name":"needle-project/laravel-rabbitmq","owner":"needle-project","description":"A simple rabbitmq library for laravel","archived":false,"fork":false,"pushed_at":"2024-10-01T11:19:00.000Z","size":202,"stargazers_count":38,"open_issues_count":7,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-28T22:56:19.614Z","etag":null,"topics":["amqp","laravel","php","php7","rabbitmq"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/needle-project.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-14T14:34:04.000Z","updated_at":"2025-06-10T11:53:11.000Z","dependencies_parsed_at":"2022-09-24T21:20:42.724Z","dependency_job_id":"571a6b3e-f73a-4c39-b1dd-951ac5dfbabb","html_url":"https://github.com/needle-project/laravel-rabbitmq","commit_stats":{"total_commits":61,"total_committers":11,"mean_commits":5.545454545454546,"dds":"0.39344262295081966","last_synced_commit":"2a6de5d90612b098e665114981b973ab6a788fe5"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/needle-project/laravel-rabbitmq","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/needle-project%2Flaravel-rabbitmq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/needle-project%2Flaravel-rabbitmq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/needle-project%2Flaravel-rabbitmq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/needle-project%2Flaravel-rabbitmq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/needle-project","download_url":"https://codeload.github.com/needle-project/laravel-rabbitmq/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/needle-project%2Flaravel-rabbitmq/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263060210,"owners_count":23407473,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amqp","laravel","php","php7","rabbitmq"],"created_at":"2024-12-06T04:13:46.953Z","updated_at":"2025-07-02T01:32:56.490Z","avatar_url":"https://github.com/needle-project.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/needle-project/laravel-rabbitmq/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/needle-project/laravel-rabbitmq/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/needle-project/laravel-rabbitmq/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/needle-project/laravel-rabbitmq/?branch=master)\n[![Total Downloads](https://poser.pugx.org/needle-project/laravel-rabbitmq/downloads)](https://packagist.org/packages/needle-project/laravel-rabbitmq)\n\n# Laravel RabbitMQ\nA simple rabbitmq library for laravel based on Publish–Subscribe pattern where the subscriber is the Consumer.\n\n# Table of Contents\n1. [Install](#1-install)\n2. [Configure](#2-configure)\n\n    2.1. [Connections](#21-connection)\n\n    2.2. [Queues](#22-queues)\n\n\t2.3. [Exchanges](#23-exchanges)\n\n\t2.4. [Publishers](#24-publishers)\n\n\t2.5. [Consumers](#25-consumers)\n\n3. [Usage](#3-usage) \n\n\t3.1. [Publishing a message](#31-publishing-a-message)\n\n\t3.2. [Consuming a message](#32-consuming-a-message)\n\n\t3.3. [Available CLI commands](#33-available-commands)\n\n\t3.4. [Custom Message Processor](#34-custom-message-processor)\n\n4. [Examples](#4-examples)\n\n5. [Contribute](#5-contribute)\n\n\t5.1 [Local Development](#51-local-development)\n\n    5.2 [Required Help](#52-required-help)\n\n6. [Special \"Thank You\"](#6-special-thank-you)\n\n\n# 1. Install\nRun: \n```bash\ncomposer require needle-project/laravel-rabbitmq\n```\nFor Laravel version 5.5 or higher the library should be automatically loaded via [Package discovery](https://laravel.com/docs/5.6/packages#package-discovery).\n\nFor Laravel versions below 5.5 you need to add the service provider to `app.php`:\n```php\n\u003c?php\n\nreturn [\n    // ...\n    'providers' =\u003e [\n        // ...\n        NeedleProject\\LaravelRabbitMq\\Providers\\ServiceProvider::class,\n    ],\n    // ...\n];\n```\n\n# 2. Configure\n* Create a new file called `laravel_rabbitmq.php` inside your Laravel's config directory.\n(Or use `artisan vendor:publish` - Read more [here](https://laravel.com/docs/5.0/packages))\n* Fill out the config based on your needs.\n\nThe configuration files has 5 main nodes: **connections**, **exchanges**,  **queues**, **publishers**, **consumers**.\n\nThey are used in the following mode:\n![Configuration Flow](docs/configuration_diagram.png)\n\nExample config:\n```php\nreturn [\n    'connections' =\u003e [\n        'connectionA' =\u003e [/** Connection A attributes */],\n        'connectionB' =\u003e [/** Connection B attributes */],\n    ],\n    'exchanges' =\u003e [\n        'exchangeA' =\u003e [\n            // Tells that the exchange will use the connection A\n            'connection' =\u003e 'connectionA',\n            /** Exchange A Attributes */\n        ],\n        'exchangeB' =\u003e [\n            // Tells that the exchange will use the connection B\n            'connection' =\u003e 'connectionB',\n            /** Exchange B Attributes */\n        ]\n    ],\n    'queues' =\u003e [\n        'queueA' =\u003e [\n            // Tells that the queue will use the connection alias A\n            'connection' =\u003e 'connectionA',\n            /** Queue A Attributes */\n        ]\n    ],\n    'publishers' =\u003e [\n        'aPublisherName' =\u003e /** will publish to exchange defined by alias */ 'exchangeA'\n    ],\n    'consumers' =\u003e [\n        'aConsumerName' =\u003e [\n            // will read messages from\n            'queue' =\u003e 'queueA',\n            // and will send the for processing to an \"NeedleProject\\LaravelRabbitMq\\Processor\\MessageProcessorInterface\"\n            'message_processor' =\u003e \\NeedleProject\\LaravelRabbitMq\\Processor\\CliOutputProcessor::class\n        ]\n    ]\n]\n```\n\n### 2.1. Connections\n**Connection attributes:**\n* All attributes are optional, if not defined the defaults will be used.\n                    \nAttribute  | Type | Default value | Description\n------------- | ------------- | ------------- | -------------\nhostname  | string | 127.0.0.1 | The host for the RabbitMQ instance\nport  | integer |5672 | The port for the RabbitMQ instance\nusername  | string | guest | Default's RabbiqMQ username\npassword | string | guest | Default's RabbiqMQ username\nvhost  | string | / | RabbitMQ Virtual Host. Read more [here](https://www.rabbitmq.com/vhosts.html).\nlazy  | boolean | true | Setting it lazy will only make the connection when an action that needs the connection willl be called\nread_write_timeout  | integer | 3 | TTL for read/write operations.\nconnect_timeout  | integer | 3 | TTL for the connection\nheartbeat  | integer | 0 | Whether to check the socket connection periodically. Read more [here](https://www.rabbitmq.com/heartbeats.html).\nkeep_alive | boolean | false | Whether to use system's keep alive property. Read more [here](https://www.rabbitmq.com/networking.html).\n\n### 2.2. Queues\n**Queue main nodes:**\n\nNode key  | Type | Description\n------------- | ------------- | -------------\nconnection | string | The reference to the connection that should be used.\nname | string | The actual name of the queue on RabbitMQ\nattributes | array | Optional attributes for the queue\n\n*Queue attributes*\n\nAttribute key  | Type | Default | Description\n------------- | ------------- | ------------- | -------------\npassive | boolean | false | This is an AMQP attribute. Read about [here] (https://www.rabbitmq.com/amqp-0-9-1-reference.html)\ndurable | boolean | false | Same as passive attribute\nexclusive | boolean | false | Same as passive attribute\nauto_delete | boolean | false | Same as passive attribute\ninternal | boolean | false | Same as passive attribute\nnowait | boolean | false | Same as passive attribute\nauto_create | boolean | false | Whether should try to create (and bind) the queue when queried.\nthrow_exception_on_redeclare | boolean | true | Throw exception when re-declare of the queue fails\nthrow_exception_on_bind_fail | boolean | true | Throw exception when cannot create the bindings\nbind | array | empty | Whether should bind to an exchange: See Example 1. \n\nExample 1:\n```php\n[\n\t['exchange' =\u003e 'first.exchange', 'routing_key' =\u003e '*'],\n\t['exchange' =\u003e 'second.exchange', 'routing_key' =\u003e 'foo_bar'],\n]\n```\n\n### 2.3. Exchanges\n**Exchange main nodes:**\n\nNode key  | Type | Description\n------------- | ------------- | -------------\nconnection | string | The reference to the connection that should be used.\nname | string | The actual name of the queue on RabbitMQ\nattributes | array | Optional attributes for the exchange\n\n*Exchange attributes*\n\nAttribute key  | Type | Default | Description\n------------- | ------------- | ------------- | -------------\nexchange_type | string | - | **Mandatory** The type of the exchange (direct / fanout / topic / headers). View description [here] (https://www.rabbitmq.com/tutorials/amqp-concepts.html)\npassive | boolean | false | This is an AMQP attribute. Read about [here] (https://www.rabbitmq.com/amqp-0-9-1-reference.html)\ndurable | boolean | false | Same as passive attribute\nauto_delete | boolean | false | Same as passive attribute\ninternal | boolean | false | Same as passive attribute\nnowait | boolean | false | Same as passive attribute\nauto_create | boolean | false | Whether should try to create (and bind) the queue when queried.\nthrow_exception_on_redeclare | boolean | true | Throw exception when re-declare of the queue fails\nthrow_exception_on_bind_fail | boolean | true | Throw exception when cannot create the bindings\nbind | array | empty | Whether should bind to an exchange: See Example 2. \n\nExample 2:\n```php\n[\n\t['queue' =\u003e 'first.exchange', 'routing_key' =\u003e '*'],\n\t['queue' =\u003e 'second.exchange', 'routing_key' =\u003e 'foo_bar'],\n]\n```\n### 2.4. Publishers\nA publisher push a message on an *exchange* (but it can also push it on a queue).\nDefining a publishers:\n```php\n'publishers' =\u003e [\n\t'myFirstPublisher' =\u003e 'echangeAliasName',\n\t'mySecondPublisher' =\u003e 'queueAliasName'\n\t// and many as you need\n]\n```\n\n### 2.5. Consumers\nA consumer will always get message from a queue.\nDefine a consumer:\n```php\n'consumers' =\u003e [\n    'myConsumerName' =\u003e [\n        'queue' =\u003e 'queueAliasName',\n        'prefetch_count' =\u003e 1,\n        'message_processor' =\u003e \\NeedleProject\\LaravelRabbitMq\\Processor\\CliOutputProcessor::class\n    ]\n]\n```\n\nField  | Type    | Description\n------------- |---------| -------------\nqueue | string  | Reference of the defined queue block.\nprefetch_count | int     | *Default: 1*. The number of the message that a cosumer will grab without ack. Read more [here](https://www.rabbitmq.com/consumer-prefetch.html)\nglobal_prefetch | boolean | *Default: true*. Setting if the prefetch should be set globally on the channel. Read more [here](https://www.rabbitmq.com/consumer-prefetch.html)\npassive | boolean | false | This is an AMQP attribute. Read about [here] (https://www.rabbitmq.com/amqp-0-9-1-reference.html)\ndurable | boolean | false | Same as passive attribute\nauto_delete | boolean | false | Same as passive attribute\ninternal | boolean | false | Same as passive attribute\nnowait | boolean | false | Same as passive attribute\nauto_create | boolean | false | Whether should try to create (and bind) the queue when queried.\nthrow_exception_on_redeclare | boolean | true | Throw exception when re-declare of the queue fails\nthrow_exception_on_bind_fail | boolean | true | Throw exception when cannot create the bindings\nbind | array | empty | Whether should bind to an exchange: See Example 2. \n\n\n# 3. Usage\nAfter configuring, you should end up with a configuration file `laravel_rabbitmq.php` similar to this one:\n\n```php\nreturn [\n    'connections' =\u003e [\n        'connectionA' =\u003e [],\n    ],\n    'exchanges' =\u003e [\n        'exchangeA' =\u003e [\n            'connection' =\u003e 'connectionA',\n\t\t\t'name' =\u003e 'foo_bar',\n\t\t\t'attributes' =\u003e [\n\t\t\t\t'exchange_type' =\u003e 'topic'\n\t\t\t]\n        ]\n\t],\n    'queues' =\u003e [\n        'queueB' =\u003e [\n            'connection' =\u003e 'connectionA',\n            'name' =\u003e 'foo_bar_listener',\n\t\t\t'attributes' =\u003e [\n\t\t\t\t'bind' =\u003e [\n                    ['exchange' =\u003e 'foo_bar', 'routing_key' =\u003e '*']\n                ]\n\t\t\t]\n        ]\n    ],\n    'publishers' =\u003e [\n        'aPublisherName' =\u003e 'exchangeA'\n    ],\n    'consumers' =\u003e [\n        'aConsumerName' =\u003e [\n            'queue' =\u003e 'queueB',\n            'message_processor' =\u003e \\NeedleProject\\LaravelRabbitMq\\Processor\\CliOutputProcessor::class\n        ]\n    ]\n]\n```\n\n### 3.1. Publishing a message\n**Example of usage in code:**\n```php\n\u003c?php\n/**\n * @var $app \\Illuminate\\Contracts\\Container\\Container\n * @var $publisher \\NeedleProject\\LaravelRabbitMq\\PublisherInterface \n */\n$publisher = $app-\u003emakeWith(PublisherInterface::class, ['aPublisherName']);\n$message = [\n    'title' =\u003e 'Hello world',\n    'body' =\u003e 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',\n];\n$routingKey = '*';\n$publisher-\u003epublish(json_encode($message), /* optional */$routingKey);\n```\nOptional, there is a command that can be used to publish a message.\n```bash\nphp artisan rabbitmq:publish aPublisherName MyMessage\n```\n*Note*: At the moment, routing key in CLI is not supported.\n\n### 3.2. Consuming a message\nConsuming message should be done by running a command in deamon mode. While PHP is not intended to do that, you can use [supervisor](http://supervisord.org/index.html) for that.\n\nThe **flow** of the consumer is rather simple:\nCLI Consumers -\u003e Get message -\u003e Passes it to the `message_processor` key from configuration.\n\nA message processor is a class that implements `NeedleProject\\LaravelRabbitMq\\Processor` interface. If you do no want to handle acknowledgement you can extend  `\\NeedleProject\\LaravelRabbitMq\\Processor\\AbstractMessageProcessor` which require implementation of `processMessage(AMQPMessage $message): bool` method.\n\nYou `message_processor` key is runned by laravel's `app` command for build of the class.\n\nStart the message consumer/listener:\n```bash\nphp artisan rabbitmq:consume aConsumerName\n```\nRunning consumers with limit (it will stop when one of the limits are reached)\n\n```bash\nphp artisan rabbitmq:consume aConsumerName --time=60 --messages=100 --memory=64\n```\nThis tells the consumer to stop if it run for 1 minute or consumer 100 messages or has reached 64MB of memory usage.\n\n### 3.3. Available commands\nWhen running `php artisan` a new namespace will be present:\n\nName | Description | Example\n--- | --- | ---\nrabbitmq:consume | Consummer command | `php artisan rabbitmq:consume [consumer-name] --time=60 --messages=100 --memory=64` Where --time/messages/memory are optional. Default values are 60 seconds, 100 messages and 64MB of RAM usage\nrabbitmq:delete-all | Delete all queues, exchanges and binds that are defined in entities AND referenced to either a publisher or a consumer | \nrabbitmq:list | List all entities by type: publishers|consumers |\nrabbitmq:publish | Publish one message using a consumer | `php artisan rabbitmq:publish [publisher-name] message`\nrabbitmq:setup | Creates all queues and exchanges | `php artisan rabbitmq:setup` or `php artisan rabbitmq:setup --force`. **NOTE** When using force, all queues and exchanges will be deleted first and then re-created.\n\n### 3.4. Custom Message Processor\nAt the current moment there is the possibility to either implement the `MessageProcessorInterface` class or extend the `AbstractMessageProcessor`.\n\nWhen using the `AbstractMessageProcessor`, you will have access to extra API than can be used in your `processMessage()`:\n```php\nprotected function ack(AMQPMessage $message);\nprotected function nack(AMQPMessage $message, bool $redeliver = true);\n```\n\n# 5. Contribute\nYou are free to contribute by submitting pull request or reporting any issue in Github.\nAt the current stage of the project, no contribution procedure is defined.\n\n## 5.1 Local Development\nRun composer install (with ignore-platform-reqs to avoid missing extensions):\n```bash\n docker run --rm -v $(pwd):/app jitesoft/phpunit:8.1 composer install --ignore-platform-req=ext-sockets\n```\n\nRun unit tests via Docker:\n```bash\ndocker run --rm -v $(pwd):/app jitesoft/phpunit:8.1 phpunit --configuration phpunit.xml\n```\n\n## 5.2. Required Help\nThere are multiple topics for which the library needs help\n- CI Pipeline: There is a need for a configuration of *scrutinizer* (or any other tool) that can cover running tests for all supported PHP Versions and Laravel Framework versions\n- Documentation: Any improvement to easy the use of the library it's welcome\n- Examples: A section of examples that proves the library's different real-world scenario examples\n\n## 6. Special \"Thank you\"\nSpecial \"Thank you\" goes out to the library [contributors](https://github.com/needle-project/laravel-rabbitmq/graphs/contributors).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneedle-project%2Flaravel-rabbitmq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneedle-project%2Flaravel-rabbitmq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneedle-project%2Flaravel-rabbitmq/lists"}