Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Broadcast with Redis not working/connecting?

So I'm trying to broadcast Laravel 5 Events with the help of Redis. No I don't wanna use a service like Pusher since it's not free (even if the free limit would be enough for me) and I wanna keep control of the broadcast server.

So what I've done so far is, I'Ve set up a redis server (listening on port 6379 -> default), I've set up the following event:

class MyEventNameHere extends Event implements ShouldBroadcast
{
    use SerializesModels;
    public $data;

    /**
     * Create a new event instance.
     *
     * @return \App\Events\MyEventNameHere
     */
    public function __construct()
    {
        $this->data = [
            'power' => 10
        ];
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['pmessage'];
    }
}

I registered a route to that event:

Route::get('test',function()
{
    event(new App\Events\MyEventNameHere());
    return "event fired";
});

I've created (more like copied :P) the node socket server:

var app = require('http').createServer(handler);
var io = require('socket.io')(app, {origins:'*:*'});

var Redis = require('ioredis');
var redis = new Redis();

app.listen(6379, function() {
    console.log('Server is running!');
});

function handler(req, res) {
    res.writeHead(200);
    res.end('');
}

io.on('connection', function(socket) {
    console.log(socket);
});

redis.psubscribe('*', function(err, count) {
});

redis.on('pmessage', function(subscribed, channel, message) {
    console.log(message);
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});

And I created the view to actually receive the broadcast (testview.blade.php):

@extends('layout')
@section('content')
<p id="power">0</p>

<script>
var socket = io('http://localhost:6379');
socket.on("pmessage:App\\Events\\MyEventNameHere", function(message) {
    console.log(message);
    $('#power').text(message.data);
});
console.log(socket.connected);
</script>
@endsection

I can launch the redis server without any problems. I can launch the node socket.js server and I'm getting the response "Server running"

When I hit the route to the event I get the return "event fired" in my browser.

When I hit the route to the actual view

Route::get('test/view',function()
{
    return view('testview');
});

I can see the whole page (layout is rendered), and the webconsole does not show any errors.

However if I fire the event, the view won't change, which means, the broadcast is not received right?

Now I included an output for the console

console.log(socket.connected);

which should show me if the client is connected to the socket.io right?

Well, the output says false. What am I doing wrong here?

Further information on my setup: I'm running the whole project on the php built-in server, the whole thing is running on Windows (if ever that could matter), my firewall is not blocking any of the ports.

EDIT 1:

I forgot to say that my node server is not receiving the messages as well... It only says "Server running", nothing else.

EDIT 2:

I used another 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');
});

And this time the console receives the messages. So if the node socket.io receives the messages, then what's wrong with my client? Obviously the messages are being broadcasted correctly, the only thing is that they are not being received by the client...

like image 444
Jan Schuermann Avatar asked Jul 07 '15 08:07

Jan Schuermann


People also ask

Could not connect to Redis at 127.0 0.1 6379 no connection could be made because the target machine actively refused it?

Firewall restriction is another common reason that can trigger the “could not connect to Redis connection refused”. By default Redis server listen to the TCP port 6379. If another application is using the port or if the firewall restrictions blocks the port, it can trigger the connection refused error.

How Redis works in Laravel?

Laravel supports the use of Redis, which uses caches for temporary data storage to speed up the process of performing database queries and getting feedback, which will, in turn, reduce the amount of time spent pulling up data.

Does Laravel need Redis?

So, Laravel has a built-in caching feature that is supported by Redis. Redis keeps its database completely in memory, using disk only for persistence. So that the response will still be slower when the data is not in Redis yet.


2 Answers

I can't say what is exactly wrong and probably no one can't, because your problem is to broad and enviroment dependent. Using Wireshark Sniffer you can easily determinate part of solution that is not working correctly and then try find solution around actual problem.

If your question is about how to do that, I will suggest not involving node on server side and use .NET or Java language.

like image 82
Svisstack Avatar answered Sep 29 '22 14:09

Svisstack


The problem with your code is you are connecting your client socket to the redis default port 6379 rather than the node port that is 3000.

So in your blade view change var socket = io('http://localhost:6379'); to var socket = io('http://localhost:3000');

like image 36
oseintow Avatar answered Sep 29 '22 16:09

oseintow