0

I am setting up Laravel API for a mobile app to call the APIs and I am currently configuring Laravel Broadcast which I am new to it. I have installed laravel-echo-server and redis for the server side while laravel-echo and socket.io-client for the client side.

As you can see, in the screenshot below, the client is subscribed to the channel.

enter image description here

Here is my code

OrderController.php

broadcast(new Ordered(Order::find($order->id)));

OrderEvent.php

public function broadcastOn()
{
     return new Channel('order');
}

channels.php

Broadcast::channel('order.{orderId}', function (User $user, int $orderId) {
    return true;
});

I tried to run node index.js in the terminal with the following codes in my index.js and there's no response from the console.log:

index.js

const express = require('express');
const Echo = require('laravel-echo');
const io = require('socket.io-client');

const echo = new Echo({
  broadcaster: 'socket.io',
  host: 'http://192.168.1.14:6001',
  client: io,
});

echo.channel('public').listen('order', (e) => {
  console.log("DATA RECEIVED");
  console.log(e);
})
doremi666
  • 121
  • 3
  • 15
  • What interfaces does your event implement? – dbf Jul 19 '20 at 15:28
  • Secondly, you are listening on the `public` channel, although you said to broadcast it on the `Channel('order');` – dbf Jul 19 '20 at 15:31
  • i have changed `echo.channel('public').listen('order', (e) => { console.log("DATA RECEIVED"); console.log(e); })` to channel order and the listener of name Ordered The client joined order channel but the Ordered listener seems like not working – doremi666 Jul 25 '20 at 07:31
  • may i know how to do the debugging here? as there's no console.log result showing out and there's no event listener of name Ordered shown in the command too. – doremi666 Jul 25 '20 at 07:34
  • Ok I'll answer in a bit, but I do need to see your event class `Ordered`. – dbf Jul 25 '20 at 08:28
  • I have added a BroadcastAs in the Ordered event listener class and call it in my client side and it works well now. Thanks anyway :) – doremi666 Jul 25 '20 at 13:14
  • That's why I needed to see your event class :D – dbf Jul 25 '20 at 15:57

0 Answers0