I have a news feed, If user X commented on user Y's post then user Y should get a notification. Now the issue is that user Y dont have to the post id on which user X commented on :
// Create comment
$comment = new Comment;
$comment->post_id = $post_id;
$comment->user_id = $user_id;
$comment->body = $body;
$comment->save();
// Save activity in database
$newUser = PostActivity::firstOrCreate([
'post_id' => $post_id,
],[
'user_id' => Auth::user()->id,
'post_id' => $post_id,
'seen' => '0'
]);
// Dispatch event with newly created comment
FeedCommentActivity::dispatch($comment);
The event :
public function broadcastOn()
{
return new PrivateChannel('post-comment-activity.' .$this->activity->post_id);
}
The channel :
Broadcast::channel('post-comment-activity.{postId}', function ($user, $postId) {
// Lets say true for the time
return true;
});
And the listener, This is where my question arises that how can postId will come up here :
From where the
postIdwill come to listen and match the channel is listening .
window.Echo.channel('post-comment-activity' + postId)
.listen('FeedCommentActivity', e => {
console.log('New comment by a user');
console.log(e);
});
I want to notify the participator or the owner of the post to be notified when a new comment comes up.
What will be the way around it ? Any alternate way ?