I'm making a real time activity feed using laravel echo, but, I only want users to see activity from other users that he follows.
What would be the best way to achieve this?
broadcasting to a public channel, that an user viewing the feed is listening to, and, when a new event comes in, it checks if the user follows this new activity's user. If yes, then the activity is inserted. I think it's not a good way because if multiple events are triggered the app would be in constant calculation if it should show the new activity or not...
listening to a private channel particular this user id, and, when a new activity is posted, it broadcasts this event to all its followers... If so, how could I make this? Wouldn't it be slow if the user has a lot of followers?
@gbdematos There are a couple of ways you could tackle this.
When you post an item, you could broadcast it to the feed of the user’s followers:
broadcast(new PostPublished($post))
class PostPublished implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $post;
public function __construct(Post $post)
{
$this->post = $post;
}
public function broadcastOn()
{
return $this->post->author->followers->map(function (User $follower) {
return "feed.{$follower->getKey()}";
});
}
}
@MARTINBEAN - Hey, thanks for your answer. I managed to make it work using the code you provided, with the only difference that I had to add ->toArray() in the end.