A Laravel package for listening to Redis published messages and handling them with jobs, events, or other handlers.
You can install the package via Composer:
composer require iankibet/redis-subAfter installation, publish the package configuration file:
php artisan vendor:publish --tag=redis-subThis will create a config/redis-sub.php file where you can define the Redis channels and their handlers.
In config/redis-sub.php, define the Redis channels and their corresponding handlers:
return [
'channels' => [
'members' => [
\App\Jobs\ProcessMemberMessage::class,
\App\Listeners\MemberListener::class,
],
'notifications' => [
\App\Events\NotificationReceived::class,
],
],
];Handlers can be:
- Jobs (e.g.,
ProcessMemberMessagethat implementsShouldQueue). - Events (e.g.,
NotificationReceivedthat uses theDispatchabletrait). - Callable Classes (e.g.,
MemberListenerwith an__invokemethod orhandlemethod).
<?php
namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldQueue;
class ProcessMemberMessage implements ShouldQueue
{
protected $message;
public function __construct($message)
{
$this->message = $message;
}
public function handle()
{
logger()->info("Processed job message: {$this->message}");
}
}<?php
namespace App\Events;
use Illuminate\Foundation\Events\Dispatchable;
class NotificationReceived
{
use Dispatchable;
public $message;
public function __construct($message)
{
$this->message = $message;
}
}<?php
namespace App\Listeners;
class MemberListener
{
public function __invoke($message)
{
logger()->info("Handled message with callable: {$message}");
}
}Run the Redis subscriber command to listen to the configured channels:
php artisan redis:subscribeThe command will listen for messages published on the Redis channels and dispatch the configured handlers.
When a message is published to the members channel, you'll see output like this:
[2024-11-24 15:30:15] Dispatched job: App\Jobs\ProcessMemberMessage for channel: members
[2024-11-24 15:30:15] Called handler: App\Listeners\MemberListener for channel: members
- Timestamps are included in log messages for better debugging.
- Ensure the Redis service is running and accessible by Laravel.
Feel free to submit issues or pull requests. Contributions are welcome!
The MIT License (MIT). Please see LICENSE for more information.
---
### Key Updates:
1. **Added Packagist Badges**: To display version, downloads, and license status.
2. **Installation Command**: Direct install command using `composer require iankibet/redis-sub`.
3. **Configuration Section**: Clear explanation of how to use the published configuration file.
4. **Usage and Debugging**: Detailed examples of expected behavior and logs.
5. **License**: Reference to the `LICENSE` file.
Let me know if you'd like additional refinements!