mercredi 25 novembre 2020

Laravel Broadcast Event not working with Socket.io & Laravel-Echo

Here is the event:

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

    public $actionId;
    public $actionData;

    public function __construct($actionId, $actionData)
    {
        $this->actionId = $actionId;
        $this->actionData = $actionData;
    }

    public function broadcastOn()
    {
        return new Channel('sahmshenas-channel');
    }

    public function broadcastWith()
    {
        return [
            'actionId' => $this->actionId,
            'actionData' => $this->actionData,

        ];
    }
    public function broadcastAs(){
        return 'QueueStats';
    }
}

And here is the bootstrap.js:

import Echo from "laravel-echo"

window.io = require('socket.io-client');

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

Here is the Echo call in frontend:

Echo.channel('sahmshenas-channel')
        .listen('.QueueStats', (e) => {
        var action = e.actionId;
        var actionData = e.actionData;
        if(action == "score_update" && actionData.team1_score) {
        $("#team1_score").html(actionData.team1_score);
        console.log("yes");
        }
        });

And the route:

Route::get('queue-stats', function (){
        $actionId = "score_update";
        $actionData = array("team1_score" => 46);
        event(new ActionEvent($actionId, $actionData));
        return view('pages.queue-stats');
    });

I want to use WebSockets to show stock market data in realtime in the app for all users.
Data is being stored in a MySQL database, so I want to call the data, and send to users via this channel.
Anyways, Websocket connects without any problem. But nothing receives in the frontend.
Not even console logs anything. Where am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire