Laravel - Redis - Pubsub
Laravel provides a convenient interface to the Redis publish
and subscribe
commands. These Redis commands allow you to listen for messages on a given "channel". You may publish messages to the channel from another application, or even using another programming language, allowing easy communication between applications and processes.
First, let's setup a channel listener using the subscribe
method. We'll place this method call within an Artisan command since calling the subscribe
method begins a long-running process:
Now we may publish messages to the channel using the publish
method:
use Illuminate\Support\Facades\Redis;
Route::get('/publish', function () {
// ...
Redis::publish('test-channel', json_encode([
'name' => 'Adam Wathan'
]));
});