Download the PHP package goodway/laravel-nats without Composer
On this page you can find all versions of the php package goodway/laravel-nats. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download goodway/laravel-nats
More information about goodway/laravel-nats
Files in goodway/laravel-nats
Package laravel-nats
Short Description Nats jetstream queue driver with client for Laravel
License MIT
Informations about the package laravel-nats
Nats JetStream queue driver for Laravel
With multiple flexible client and queue configurations, with events and customizable handlers
Feel free to contribute or give any feedback.
- Prerequisites
- Installation
- Configuration
- Publishing
- Listening
- Message
- Events
Prerequisites
Laravel Version
This package can be used in Laravel 8 or higher. The minimum PHP version required is 8.1
Config file
This package publishes a config/nats.php file. If you already have a file by that name, you must rename or remove it, as it will conflict with this package. You could optionally merge your own values with those required by this package, as long as the keys that this package expects are present. See the source file for more details.
Nats Client
As a Nats client we use an external basis-company/nats.php package - the most popular, well-written and functional Nats client for PHP. Greatest thanks to Dmitry Krokhin (nekufa)!
Installation
The recommended way to install the library is through Composer:
You should publish the config/nats.php config file with:
Configuration
Client connection
The client connection configuration is specified in the config/nats.php file. Multiple configuration supported
Queue connection
Describe the queue connection configuration in your 'config/queue.php' file. It supports multiple client configurations.
Example:
Fields description:
- driver - [string] queue driver name. Use 'nats'
- consumer_client - [string] client configuration name used to listen queue
- publisher_client - [string] client configuration name used to publish messages
- jetstream - [string] nats jetstream name
- jetstream_retention_policy - [string] jetstream retention policy. Used on queue listening to generate the correct consumer name
- consumer - [string] consumer group. Used for the final name of the consumer
- consumer_iterations - [int, optional, 2] how many times message request should be sent
- consumer_delay - [float, optional, 1] how long to wait (in sec.) before sending the next request if an empty response was received
- queue_consumer_create - [bool] if true, Queue will try to automatically create a new consumer if one is not found. This functionality only works if the current connection client has the necessary permissions to create a consumer
- queue_consumer_prefix - [string] consumer prefix. Used for the final name of the consumer
- queue_separated_clients - [bool] see description below
- fire_events - [bool] if true then events will be fired during the publishing and handling received messages processes
- default_batch_size - [int, optional, 10] batch size. How many messages would be requested from nats stream
- queue_handler - [string, optional] classname of your custom queue handler. If not defined, then the standard handler will be used
- verbose_mode - [bool, optional, false] var_dump some additional info. For example on publishing
- check_jetstream_publish [bool, optional, true] - additionally check for the existence of a jetstream with each PUB
You can specify one connection for publisher and another one for consumer, or use one connection for both roles.
You can also use one or separate connections if the publisher client configuration name matches the consumer client configuration. Use this feature through the "queue_separated_clients" bool attribute.
Publishing to queue
The easiest way to send a message to queue is to call a dispatch() method on a simple class that extends NatsMessageJob. The class must contain a body() method that will return the contents of the message. Return type for body() is string.
Example:
You can also use a dynamic body by passing content through the constructor.
NatsMessageJob class uses Dispatchable and Queueable concerns and implements classic ShouldQueue interface. Examples:
You can also specify the subject and jetstream for message using the \$subject and \$jetstream variables
or
DispatchNats
This package provides an additional dispatch mechanism and a corresponding helper with the dispatchNats() function. It supports additional functions designed to make it easier to understand and interact with publishing.
With dispatchNats You can set a specific jetstream or subject when dispatching a job. And also specify whether to call events.
Examples:
With helper function
With class static function
Headers
You can set a message headers using the headers() method of your class. Example:
Listening from queue/jetstream subject
You can connect and listen to messages from the queue using standard queue:work mechanism. Example:
queue:work command options
This package also extends the queue:work command and provides additional options that allow you to specify a jetstream and consumer you want to connect to, as well as to set the batch size for reading messages.
--consumer option ignores the --queue option. In this case, the --queue option will not be used in the connection logic, it is only needed for visual recognition of the queue for the developer.
Message object structure
Events
These events are fired during the publishing and listening processes.
NatsQueueMessageSent
This event is fired after a message is sent to the queue
NatsQueueMessageReceived
This event is fired when a message is received from the queue
More documentation will be added soon...
...