node.js - Broadcasting event error in Laravel 5.1 using Redis -
i've started use laravel 5.1 , pretty awesome, wanted play around new 'broadcasting event' feature using nodejs server , redis driver following guide here: http://blog.nedex.io/laravel-5-1-broadcasting-events-using-redis-driver-socket-io/. when fire event implements shouldbroadcast interface receive error: "error while reading line server. [tcp://127.0.0.1:4365]"
4365 - port server running on (listening in port). have idea why happend?
i tried use redis directly:
$redis = redis::connection(); $redis->publish('test-channel', 'msg');
got same result, "error while reading line server. [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'); });
config\database.php:
'redis' => [ 'cluster' => false, 'default' => [ 'host' => '127.0.0.1', 'port' => 4365, 'database' => 0, 'timeout' => 100, ], ],
tried change defulat timeout, set 0, -1 or >10 tried disable xdebug in php.ini problem persists.
i made debug code trying undertand might cause problem , fails in class: predis\connection\streamconnection
public function read() { $socket = $this->getresource(); $chunk = fgets($socket); if ($chunk === false || $chunk === '') { $this->onconnectionerror('error while reading line server.'); } ...
chunk false why? , why redis client trying read data server, understanding should 'publish' data server, means should write (broadcast) not read..
i think there misunderstanding of redis , does.
redis open source, bsd licensed, advanced key-value cache , store.
it used interprocess communication (ipc) it's nice publish , subscribe commands.
so laravel , node, redis enables communication between permanently running process of node , laravel. laravel publishs events redis , node subscribes specific channel of redis. here can emit message via socket connection channel names same.
to working have to following (very explained in docs):
- install redis on system. standard port 6379.
- set dependency in composer.json: predis/predis ~1.0
- configure redis in config/database.php.
- set redis in config/broadcasting.php default broadcaster.
- set default queue driver in config/queue.php. (note: event publishing doesn't work me if set redis queue driver. direct publishing working
redis::publish('test-channel', json_encode(['foo' => 'bar']));
. maybe bug? - now can create , fire events implement
shouldbroadcast
. testing purposes can use this:redis::publish('test-channel', json_encode(['foo' => 'bar']));
now time switch on node: code of socket.js looks good, assumed redis on port 6379. if node running locally can connect node server in browser so: localhost:4365
.
here's code testing node , socket.io in front- , backend:
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>
i hope helps bit :)
Comments
Post a Comment