redis pub/sub subsribe returning connection error

2.4k views Asked by At

I am on Laravel 5.1 and following the guide here: http://laravel.com/docs/5.1/redis#pubsub

I created a simple socket.io server and on client side i emitted a message to my-channel, socket.io server is able to log the message.

However, I ran the command i made for redis pub/sub, it does not recieved anything when client side has emitted a message. After a while, an error is thrown:

[Predis\Connection\ConnectionException] Error while reading line from the server. [tcp://127.0.0.1:6379]

Then i tried to use publish method in the command, it works. socket.io server is able to log the message.

Here's my console command

<?php

namespace App\Console\Commands;

use Illuminate\Support\Facades\Redis;
use Illuminate\Console\Command;

class ChannelSub extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'channel:sub';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        Redis::subscribe(['my-channel'], function($message) {
            $this->info($message);
        });
    }
}

Try it with

php artisan channel:sub

I am using predis/predis for Redis support in Laravel.

1

There are 1 answers

1
Ícaro Souza On

Laravel 5.2 change on config/database.php

'redis' => [

    'cluster' => false,

    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
    'subscribe' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
        'read_write_timeout' => 0
    ],

],