使用Redis在Laravel 5.1中广播事件错误

我已经开始使用Laravel 5.1,它非常棒,只是想通过使用NodeJs作为服务器和Redis作为驱动程序来遵循这个指南来实现新的“Broadcasting Event”function: http://blog.nedex。 io / laravel-5-1-broadcasting-events-using-redis-driver-socket-io / 。 当我触发实现ShouldBroadcast接口的事件时,我收到一个错误:“从服务器读取行时出错[tcp://127.0.0.1:4365]”

4365 – 服务器正在运行的端口(侦听该端口)。 你有什么想法为什么会发生?

我也试着直接使用Redis:

$redis = Redis::connection(); $redis->publish('test-channel', 'msg'); 

得到相同的结果,“从服务器读取行时出错[tcp://127.0.0.1:4365]”。

socket.js:

 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var Redis = require('ioredis'); var redis = new Redis(); redis.subscribe('test-channel', function(err, count) { }); redis.on('message', function(channel, message) { console.log('Message Recieved: ' + message); message = JSON.parse(message); io.emit(channel + ':' + message.event, message.payload); }); http.listen(4365, function(){ console.log('Listening on Port 4365'); }); 

configuration\ database.php中:

  'redis' => [ 'cluster' => false, 'default' => [ 'host' => '127.0.0.1', 'port' => 4365, 'database' => 0, 'timeout' => 100, ], ], 

试图改变defulat超时,将其设置为0,-1或> 10也试图禁用php.ini中的Xdebug问题依然存在。

我对代码进行了一些深入的debugging,试图找出可能导致这个问题的原因,并在类中失败:Predis \ Connection \ StreamConnection

 public function read() { $socket = $this->getResource(); $chunk = fgets($socket); if ($chunk === false || $chunk === '') { $this->onConnectionError('Error while reading line from the server.'); } ... 

大块是错误的为什么? 为什么redis客户端试图从服务器读取数据,因为我的理解是应该将数据“发布”到服务器,意味着它应该写(广播)而不是读取。

我认为对Redis的做法有误解。

Redis是一个开源的BSD许可的高级键值caching存储

它通常用于进程间通信(IPC),因为它有很好的PUBLISH和SUBSCRIBE命令。

因此,通过Laravel和Node,Redis可以实现Node和Laravel的永久运行。 Laravel将其事件发布给Redis,Node则订阅Redis的特定频道。 在这里你可以通过套接字连接发送消息,很可能通道名称是相同的。

要做到这一切,你必须得到以下内容( 在文档中有很好的解释 ):

  1. 在您的系统上安装Redis。 标准端口是6379。
  2. 在composer.json中设置依赖关系:predis / predis〜1.0
  3. 在config / database.php中configurationRedis。
  4. 将config / broadcasting.php中的Redis设置为Default Broadcaster。
  5. 设置您的默认队列驱动程序在config / queue.php。 (注意:如果将Redis设置为队列驱动程序,则事件发布不适用于我。但直接发布正在运行Redis::publish('test-channel', json_encode(['foo' => 'bar']));也许它是一个错误?
  6. 现在您可以创build并触发实现ShouldBroadcast事件。 出于testing的目的,你可以使用这个: Redis::publish('test-channel', json_encode(['foo' => 'bar']));

现在是切换到Node的时候了:你的socket.js代码看起来不错,假设Redis在6379上。如果Node在本地运行,你可以在你的浏览器中连接到你的Node服务器,如下所示: localhost:4365

这是我的代码在前端和后端testingNode和Socket.io:

socket.js

 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var Redis = require('ioredis'); var redis = new Redis(); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); redis.subscribe('test-channel', function () { console.log('Redis: test-channel subscribed'); }); redis.on('message', function(channel, message) { console.log('Redis: Message on ' + channel + ' received!'); console.log(message); message = JSON.parse(message); io.emit(channel, message.payload) }); io.on('connection', function(socket){ console.log('a user connected'); socket.on('disconnect', function(){ console.log('user disconnected'); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); 

的index.html

 <!doctype html> <html> <head> <title>Socket.IO</title> </head> <body> <ul id="messages"> <li>Hardcoded Message</li> </ul> <script src="/socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script> var socket = io(); socket.on("test-channel", function(message) { console.log(message); $('#messages').append($('<li>').text(message)); }); </script> </body> </html> 

我希望这个能有一点帮助 :)

如果你收到上面的错误,在x秒后(不是直接的),那么你需要在你的database.phpconfiguration文件中设置读写超时。

如果这是为了作为后台任务运行,像下面这样的工作将起作用:

 'redis' => [ 'cluster' => false, 'default' => [ 'host' => '127.0.0.1', 'port' => '6379', 'database' => 0, 'read_write_timeout' => -1 ], ],