Support the ongoing development of Laravel.io →
posted 7 years ago

Hi I am working on a twitter style project where a user can have followers. When that user posts something, his followers should see his post(not all the users, only those who follow that user).

So following the official laravel pusher tutorial to build a chat app, I was able to broadcast new post event to all the users but, I should be broadcasting this event to only those users who are following that particular user, who just created a new post.

Following laravel documentation, I got no success, any help would be appreciated. Here I have listed the controller, event class, channel and user relationships. I am using React in the frontend.

PostController.php


namespace App\Events;

use App\Post;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PostCreated implements ShouldBroadcast {
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $post;
    public $user;

    public function __construct(Post $post, User $user) {
        $this->post = $post;
        $this->user = $user;
    }

    public function broadcastOn() {
        return [
            new PrivateChannel('new-post'),
            new PrivateChannel('App.User.' . $this->post->user->id),
        ];
    }

    public function broadcastWith() {
        return [
            'post' => array_merge($this->post->toArray(), [
                'user' => $this->post->user,
            ]),
            'user' => $this->user,
        ];
    }
}

PostCreated.php


namespace App\Events;

use App\Post;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PostCreated implements ShouldBroadcast {
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $post;
    public $user;

    public function __construct(Post $post, User $user) {
        $this->post = $post;
        $this->user = $user;
    }

    public function broadcastOn() {
        return [
            new PrivateChannel('new-post'),
            new PrivateChannel('App.User.' . $this->post->user->id),
        ];
    }

    public function broadcastWith() {
        return [
            'post' => array_merge($this->post->toArray(), [
                'user' => $this->post->user,
            ]),
            'user' => $this->user,
        ];
    }
}

routes/channel.php

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('new-post', function ($user) {
    return Auth::check();
});

User.php


namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable {
    use Notifiable;

    protected $fillable = [
        'username', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts() {
        return $this->hasMany(Post::class);
    }

    public function getRouteKeyName() {
        return 'username';
    }

    public function isNotTheUser(User $user) {
        return $this->id !== $user->id;
    }

    public function isFollowing(User $user) {
        return (bool) $this->following->where('id', $user->id)->count();
    }

    public function canFollow(User $user) {
        if (!$this->isNotTheUser($user)) {
            return false;
        }
        return !$this->isFollowing($user);
    }

    public function canUnfollow(User $user) {
        return $this->isFollowing($user);
    }

    public function following() {
        return $this->belongsToMany('App\User', 'follows', 'user_id', 'follower_id');
    }

    public function followers() {
        return $this->belongsToMany('App\User', 'follows', 'follower_id', 'user_id');
    }

}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ryan kaloraat Joined 8 Apr 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.