Broadcast::channel Callback Function Is Not Called
I have a problem where event is properly fired, but the data is not returned from authorization callback function inside Broadcast::channel method. The event looks like this: publ
Solution 1:
As far as I know, the channel.php
is for Authorization Logic.
Where you return data is broadcastWith()
- or if you don't have broadcastWith()
method, public properties will be pushed.
It should be something like:
In channels.php
, it's for authorization of the socket channel, so it should return a boolean.
Broadcast::channel('fixture-channel.{fixtureId}', function ($user, $fixtureId) {
return$user->canAccess($fixtureId);
// this should return a boolean.
});
The rest are fine to be done in construct. Whatever of your properties are defined as public will be pushed.
public$message;
public$username;
// etc..publicfunction__construct($userId, $message, $username, $profileImg, $fixtureId)
{
$this->message = $message;
$this->username = $username;
$this->profileImg = $profileImg;
$this->userId = $userId;
$this->fixtureId = $fixtureId;
}
Optionally, let's say you want to push only message
and username
, then use broadcastWith()
.
publicfunctionbroadcastWith() {
return [
'message' => $this->message,
'username' => $this->username
]
}
If the purpose is writing in database when a message is broadcasted, you can just put it in
publicfunctionbroadcastOn()
{
\DB::table('test')->insert(array('id' => '4'));
returnnew PresenceChannel('fixture-channel.' . $this->fixtureId);
}
Post a Comment for "Broadcast::channel Callback Function Is Not Called"