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 AppEvents;
use Config;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateContractsBroadcastingShouldBroadcastNow;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesArtisan;
use IlluminateSupportFacadesBroadcast;
use IlluminateSupportFacadesConfig 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 ?
2
Answers
I can able to see some problems with this code channel name is correctly
concatinated with dot
inchannels.php
but not where your’e returning presence channel so modify your code like in Event Class(Message.php)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.
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 ..!
You need to be authenticated when you use presence channel because technically Presence channel is using private channel docs here
And you have a bit of mistake in you broadcastOn method, it should be:
I have created a free websocket server that uses pusher protocol v7 you might be interested. https://soket.uk