Laravel 5.1 works with redis

Here is my Routes.php

Route::get('hello1',function(){
    Redis::publish('test-channel', json_encode(['foo' => 'bar']));
});

      

here is my app.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(6379, function(){
    console.log('listening on *:6379');
});

      

my 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>

      

now whenever php artisan redis: subscribe or hello1 route is run I get an error like

ConnectionException at line AbstractConnection.php 146: An error occurred while reading a line from the server. [TCP: // localhost: 6379]

+3


source to share


1 answer


Redis listens on port 6379 by default, and you've configured your HTTP server to listen on the same port. It won't work, will you get an error when trying to run your node app? If you don't get the error, redis is most likely not working at all or listening on a different port.

If your redis instance is listening on a different port, laravel will try to connect to redis on port 6379 and hit the node server and therefore will not be able to connect to redis as there is no redis but a node HTTP server listening on that port.

If your redis instance is listening on a different port and you changed it in your laravel config, you will have to change the way ioredis connects to redis by passing the port:

for example

var redis = new Redis(6380) 

      



For more details on connecting ioredis to redis see the github section .

As a conclusion, make sure that

  • Redis works
  • Your http server is listening on a different port than redis.
  • The connection settings for redis are correct in config / database.php and config / broadcast.php (laravel) and for node in server.js.

Note that it is not the http server that connects to redis, ioredis is.

+2


source







All Articles