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.
2.1 Download Laravel 8 to your folder: D:/Project
composer create-project laravel/laravel:^8.0 laravel80
2.2 Create virtual host to your Project: D:/Project/laravel80
3.1 Make command to subscribe channel
3.1.1 php artisan make:command RedisSubscribe
3.1.2 Edit app/Console/Commands/RedisSubscribe.php
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class RedisSubscribe extends Command
{
protected $signature = 'redis:subscribe';
protected $description = 'Subscribe to a Redis channel';
public function __construct()
{
parent::__construct();
}
public function handle()
{
Redis::subscribe(['trueme-channel'],
function (string $message) {
echo $message;
});
}
}
3.2 Create Route to push message
to trueme-channel on Redis
3.2.1 Add more this code below in to D:\YourProject\routes\web.php
<?php
...
..
.
use Illuminate\Support\Facades\Redis;
Route::get('/publish', function () {
Redis::publish('trueme-channel', json_encode([
'name' => 'This is message is publish from laravel'
]));
});
3.3 Install predis/predis package for Laravel
3.3.1 Run composer require predis/predis
3.3.2 Uncomment redis in config/app.php
...
'Redis' => Illuminate\Support\Facades\Redis::class,
...
3.3.3 Change config/database.php
...
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
...
]
...
3.4 Config redis server in Laravel
3.4.1 Config: config/database.php
...
redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
3.4.2 Update : .env
...
REDIS_HOST=host.docker.internal
REDIS_PASSWORD=
REDIS_PORT=63799
REDIS_DB=0
4.1 Run redis:subscribe command
4.2 Run http://laravel80.local/publish
4.3 Check results on command step 4.1
$ php artisan redis:subscribe
{"name":"This is message is publish from laravel"}
...
4.4 Check Subscribe channel on Redis-cli
4.5 Goto Redis-cli
# redis-cli
127.0.0.1:6379>
4.6 Get all channels are subscribed
127.0.0.1:6379> pubsub channels
1) "laravel_database_trueme-channel"
127.0.0.1:6379>
4.7 Push message from redis-cli
127.0.0.1:6379> pubsub channels
1) "laravel_database_trueme-channel"
127.0.0.1:6379> Publish laravel_database_trueme-channel "This is message is push from redis-cli"
(integer) 1
127.0.0.1:6379>
4.7 Check command screen at step 4.1
$ php artisan redis:subscribe
This is message is push from redis-cli
6.1 Check redis is ready
$ node -v
v20.7.0
6.2 Create Your Folder: D:\Projects\NodeRedis
6.3 Install redis client: $ npm install redis
6.4 Publish message to Redis
const redis = require('redis');
const publisher = redis.createClient({
socket: {
port: 63799, // Change to Your Port Redis
host: 'host.docker.internal' // Change to your host Redis
}
});
(async () => {
const article = {
id: '123456',
name: 'Publish message to Redis with Node.js',
blog: 'Trueme blog name',
};
await publisher.connect();
await publisher.publish('laravel_database_trueme-channel',
JSON.stringify(article));
})();
7.1 Download Source
https://github.com/truemenews/NodePushlish2Redis
7.2 Install dependancy package
npm install
7.3 Config for Port/host Redis client in step 6.4
7.4 Run node server.js
7.5 Check result on step 4.1
$ php artisan redis:subscribe
{"id":"123456","name":"Publish message to Redis with Node.js","blog":"Trueme blog name"}
Note: If you can't get message is published at this step, please re-run Step 4.1 && Step 7.4