Laravel function "event" not working when broadcasting an event

1.4k views Asked by At

I have the following code:

/routes/web.php

<?php

use App\Events\UserSignedUp;

Route::get('/', function () {
    $data = [
        'event' => 'UserSignedUp',
        'data' => [
            'username' => 'JohnDoe'
        ]
    ];
    //Redis::publish('test-channel', json_encode($data));
    event(new UserSignedUp('JohnDoe'));
    return view('welcome');
});

where I'm using the function: event(...).

I also have the following Event file:

/app/Events/UserSignedUp.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class UserSignedUp implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $username;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($username)
    {
        $this->$username = $username;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('test-channel');
    }

}

Then, I'm listening the event on a Node.js file:

/socket.js

var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var port = process.env.PORT || 3000;

var redis = new Redis();

redis.subscribe('test-channel');

redis.on('message', function(channel, message) {
    console.log(channel, message);
});

server.listen(port, function(){
    console.log('listening on *:' + port);
});

My problem is that when on the file above: /routes/web.php I use:

event(new UserSignedUp('JohnDoe'));

the event doesn't arrive to the Node.js listener. However, the event is arriving properly to Redis because I can see it with: $ redis-cli monitor command.

[EDIT 1] The following is what I get when the event(...) function is called:

1541489387.307573 [0 127.0.0.1:52109] "SELECT" "0"
1541489387.308379 [0 127.0.0.1:52109] "RPUSH" "queues:default" "{\"displayName\":\"App\\\\Events\\\\UserSignedUp\",\"job\":\"Illuminate\\\\Queue\\\\CallQueuedHandler@call\",\"maxTries\":null,\"timeout\":null,\"timeoutAt\":null,\"data\":{\"commandName\":\"Illuminate\\\\Broadcasting\\\\BroadcastEvent\",\"command\":\"O:38:\\\"Illuminate\\\\Broadcasting\\\\BroadcastEvent\\\":7:{s:5:\\\"event\\\";O:23:\\\"App\\\\Events\\\\UserSignedUp\\\":2:{s:8:\\\"username\\\";N;s:6:\\\"socket\\\";N;}s:10:\\\"connection\\\";N;s:5:\\\"queue\\\";N;s:15:\\\"chainConnection\\\";N;s:10:\\\"chainQueue\\\";N;s:5:\\\"delay\\\";N;s:7:\\\"chained\\\";a:0:{}}\"},\"id\":\"7CjGqwcRXoZhVIJyFU8AcQ5aTW8G6Cke\",\"attempts\":0}"

In the other hand, when I use:

Redis::publish('test-channel', json_encode($data));

The event arrives properly to the Node.js listener.

[EDIT 2] The following is what I get when the Redis::publish(...) function is called:

1541489590.930114 [0 127.0.0.1:52124] "SELECT" "0"
1541489590.930872 [0 127.0.0.1:52124] "PUBLISH" "test-channel" "{\"event\":\"UserSignedUp\",\"data\":{\"username\":\"JohnDoe\"}}"

Any idea about why the event is not arriving to the Node.js listener event though is arriving to the Redis server first?

Thanks.

0

There are 0 answers