I am very new to the laravel Event Broadcasting Concept ,i have some requirement without refreshing page i need to update the data in Angular site.i made a presence channel by using pusher.i called my broadcast in my queue class once it's triggered i got an API message from the Pusher Debugger console with what data it's triggered until this part fine.First of all i want to know weather server side implementation code and logic was correct if wrong means can you help to fix ?
broadcast(new Message('57585');
<?php
namespace App\Events;
use Config;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Config as FacadesConfig;
class Message implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $message;
public function __construct($message)
{
$this->message= $message;
}
public function broadcastOn()
{
return new PresenceChannel('test'.$this->message);
}
}
channels.php
Broadcast::channel('test.{message}', function ($user, $message) {
return true;
});
I am getting in Pusher console like my main doubt is weather this implementation is correct or not.if possible can you give some brief about concept ?
I can able to see some problems with this code channel name is correctly concatinated with dot
in channels.php
but not where your'e returning presence channel so modify your code like in Event Class(Message.php)
public function broadcastOn()
{
return new PresenceChannel('test.'.$this->message);
or
return new PresenceChannel('test'.'.'.$this->message);
}
And after this your'e using presence channel so you must authorize the user and return the user details if the user is valid otherwise null,simply can't able to return true or false According to laravel docs.
Broadcast::channel('test.{message}', function ($user, $message) {
$user_valid = //write your authorization logic which will return boolean
if($user_valid){
return $user ; //if you don't want entire user object we can pass specific variables inside array
}
});
For code cleaner you can write Authorization logic inside Channel Class or you can create in User model as per your wish.For more info Check docs
Hope This will work ..!