Sunday, March 5, 2017

Passing data to broadcast authorization callback in Laravel

I'm new in Laravel broadcasting, currently I'm using 5.4. I want to update account dashboard which contain relevant post by their interest when somebody else create new post, I'm using private channel. I succeed to broadcast every new post which posted by another user in current logged user dashboard, I need to check if new post is relevant or not, if it is then broadcast if not, don't.

I think I can achieve with filtering in authorization event, inside BroadcastServiceProvider I have wrote.

public function boot(){
     Broadcast::routes();
     Broadcast::channel('discovery.post', function ($user) {
        return true;
     });
}

I've read documentation that we could pass data along with channel name such as

Broadcast::channel('discovery.post.{postId}', function ($user, $postId) {
    // get post category, tags, etc compare with user interest 
    return $isInterest;
});

But this approach must pass new post ID in laravel Echo as well

window.Echo.private('discovery.post.{how-i-get-this-id?}')
   .listen('NewPostDashboard', (data) => {
      // add new update to dom
   }); 

How I can solve this problem, I want keep my channel name discovery.post, I want send just to user with relevant interest, I need instance of Post to check if logged user is interest in this post.

Let me know if my question is unclear before vote me down. Thank you.



via Angga Ari Wijaya

Advertisement