N
N
nioterzor2017-02-06 23:01:28
JavaScript
nioterzor, 2017-02-06 23:01:28

In laravel echo, for some unknown reason, the event handler does not fire?

There is an event

<?php

namespace App\Events;

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

class FieldWasUpdated implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;
    public $instance;
    public $key;
    public $value;

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

/**
 * Get the channels the event should broadcast on.
 *
 * @return Channel|array
 */
public function broadcastOn()
{
    return new Channel("model." . $this->instance->getModelType() . "." . $this->instance->id);
}


    //public function broadcastAs() {
    //    return "FieldWasUpdated"; commented out for testing
    //}
}

Code on the client
window.Echo = new window.LaravelEcho({
            broadcaster: 'socket.io',
            host: window.location.hostname + ':3000'
        });
        // I use different types of `listen` for testing
        Echo.channel("model.ANNOUNCEMENT.2")
            .listen(".*", function(e) {
                console.log(e);
            })
            .listen("*", function(e) {
                console.log(e);
            })
            .listen("FieldWasUpdated", function(e) {
                console.log(e);
            })
            .listen(".FieldWasUpdated", function(e) {
                console.log(e);
            })

node server code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();


http.listen(3000, function(){
console.log('Listening on Port 3000');
});

redis.psubscribe('*', function(err, count) {
console.log(err, count);
});
redis.on('pmessage', function(subscribed, channel, message) {
    // different types of emit for testing
    message = JSON.parse(message);
    const ev = channel + ":" + message.event;
    io.emit(ev, message.data);
    io.emit(channel, message);
    io.emit(message, channel);
    io.emit(channel, message.event, message.data);
    io.emit(channel, message);
    io.emit(channel, message.data, message.event);
});

In chrome, when the event fires, you can see that io.emit worked all
https://i.stack.imgur.com/gYIHR.png
When I uncomment the broadcastAs function, nothing changes.
I rename channel - nothing changes.
There are many examples on the Internet when laravel-echo does not start for people with a half kick, but here the problem is that events reach the browser, but handlers are not called. Lots of different io.emit and listen because none of them work anyway)
Where am I wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nioterzor, 2017-02-06
@nioterzor

Solution on the server

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

On the client
Echo.channel("test-channel")
            .listen("FieldWasUpdated", function(e) {
                console.log(e);
            })
            .listen(".App.Events.FieldWasUpdated", function(e) {
                console.log(e);
            })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question