Broadcast::channel callback function is not called

亡梦爱人 提交于 2020-01-25 10:21:07

问题


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:

public function __construct($userId, $message, $username, $profileImg, $fixtureId)
{
    $this->message = $message;
    $this->username = $username;
    $this->profileImg = $profileImg;
    $this->userId = $userId;
    $this->fixtureId = $fixtureId;
}

The channel (presence) is created and broadcasted like this:

public function broadcastOn()
{
    return new PresenceChannel('fixture-channel.' . $this->fixtureId);
}

Then in BroadcastServiceProvider class, I call this:

public function boot()
{

    Broadcast::routes(['middleware' => ['auth']]);

    require base_path('routes/channels.php');
}

The channels.php file where the problematic function is, looks like this:

Broadcast::channel('fixture-channel.{fixtureId}', function ($user, $fixtureId, $message, $username, $profileImg, $userId) {
    DB::table('test')->insert(array('id' => '4'));
    return [
        'message' => $message, 
        'username' => $username, 
        'profileImg' => $profileImg, 
        'userId' => $userId, 
        'fixtureId' => $fixtureId
    ];
});

As you see, I'm trying to insert some data inside the callback function, but that never happens. Outside the callaback function, the data is successfully inserted. I also tried with a callback function where only user and fixtureId are defined - no success.

Thank you in advance.


回答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..

public function __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().

public function broadcastWith() {
   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

public function broadcastOn()
{
    \DB::table('test')->insert(array('id' => '4'));

    return new PresenceChannel('fixture-channel.' . $this->fixtureId);
}


来源:https://stackoverflow.com/questions/59347105/broadcastchannel-callback-function-is-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!