5

I read here : https://laravel.com/docs/5.4/broadcasting#concept-overview

There explains that it can use public channel. But there is no example of how to use it

I just see an example of a private channel like this :

public function broadcastOn()
{
    return new PrivateChannel('user.'.$this->user->id);
}

I need a real example of public channel

I've tried searching, but I have not found it yet

Is there anyone can help?

moses toh
  • 12,344
  • 71
  • 243
  • 443

1 Answers1

19

if the Channel class is not used in your event, add this line:

use Illuminate\Broadcasting\Channel;

you have to return a Channel:

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

An example can be found here: https://petericebear.github.io/starting-laravel-echo-20170303/

cre8
  • 13,012
  • 8
  • 37
  • 61
  • I say without anything authentication. Means there should not be this : `$this->user->id`. Your code is still using it – moses toh Aug 26 '17 at 12:50
  • updated the code so now it has nothing to do with authentication. When you have a private channel, you must also authorized to join it. – cre8 Aug 26 '17 at 12:52
  • Okay. But i'm still confused your code. This : `myPublicChannelName`. Where it is used? – moses toh Aug 26 '17 at 13:04
  • You can listen to new events on this channel with your client. Because you could have multiple channels, but want to listen to just one of them. – cre8 Aug 26 '17 at 13:13
  • 2
    Okay. Whether on the client side like this? `Echo.channel('myPublicChannelName') .listen('...', (e) => { console.log(e.order.name); });`. – moses toh Aug 26 '17 at 13:22
  • Okay. I want ask again. This : `.listen('abc'`, Where `abc` is used? – moses toh Aug 26 '17 at 13:27
  • You listen to your events on a channel https://laravel.com/docs/5.4/broadcasting#listening-for-events. `abc` would be your laravel event, where you `boradcaston` method is located. – cre8 Aug 26 '17 at 13:30
  • Okay. Is it like this ? `public function broadcastAs() { return 'abc'; }` – moses toh Aug 26 '17 at 13:34
  • No, `abc` is your EventClass name: `class abc implements ShouldBroadcast`. If you read the official documentation or the link I posted in my answer you will find all answers :) – cre8 Aug 26 '17 at 13:36
  • I want ask again. Does it use listeners? Listeners here : `\myshop\app\Listeners\...`. Whether it needs to be listed here : `\myshop\app\Providers\EventServiceProvider.php`. Because every time using the event must use listener and register first through `EventServiceProvider`. But in the reference you provided, it does not exist – moses toh Aug 27 '17 at 06:59
  • No, you do not need them for broadcasting. They are only needed if you want to listen to fired events with Laravel to do some things in the background. You only need them, when you use notifications, where you can broadcast to the notificationchannel of a special user, but you want to broadcast to a public channel so this is not relevant. I think not every event needs a listener and does not be added to the provider, but I have not checked it yet. – cre8 Aug 27 '17 at 07:19
  • Okay. Thanks for your help. But I'm still fail. I make the new question which describes the details of my problem. Look at this : https://stackoverflow.com/questions/45903508/why-broadcasting-channel-public-not-working-laravel. I had follow your reference – moses toh Aug 27 '17 at 09:29