vincent15000's avatar

Customize the broadcasted data according to users' rights

Hello,

I have one event which has to be broadcasted to several users, but not with the same data.

Example with an event sending some data related to an endpoint.

  • user A : all the endpoint properties

  • user B : only the endpoint id

// event EndpointUpdated

public function broadcastOn(): array
{
    $channels = [];

    foreach ($this->users as $user) {
        $channels[] = new PrivateChannel('users.'.$user->id);
    }

    return $channels;
}

public function broadcastWith(): array
{
	// return different data according the private channel / user id
}

I explain a bit more : user A and user B are connected, user A saves a change in some data => user A receives an event and user B receives an event, but not with the same data.

How can I customize the data according to the channel / user id ?

Thanks for your help.

V

0 likes
3 replies
s4muel's avatar

AFAIK the broadcastWith() output is sent to all channels, so you probably cannot do that by a simple tweak.

i would modify the EndpointUpdated to just broadcast to a single private channel (single user, not an array of all users/channels) and therefore you can just construct the output in broadcastWith() by checking the role/updated_by or whatever. and then dispatch the EndpointUpdated for every (listening) user.

i am not very skilled in broadcasting, this is just something what i immediately though of. i have no idea of the performance impact, but if you have many users i think you can just broadcast two* events. one for the updater with full data and one for the rest of the users, just with the limited data.

1 like
vincent15000's avatar

@s4muel I'm not sure to understand what you mean.

According to my example, when user A changes the datas, user B will received some data via the event, but according to his own rights, ie according to user B's rights.

When user A changes the datas, for Laravel the connected user is user A. But user B has to receive an event according to his own rights.

1 like
s4muel's avatar
s4muel
Best Answer
Level 50

@vincent15000 to simplify my response, if you have two sets of users (A, B) and two sets of data to send to them:

  • tweak the EndpointUpdated event to support two formats of output data:
    • full details, for the user (A) that changes the data
    • limited details, for the others (B)
    • do this by setting a property via a constructor (and switch accordingly in the broadcastWith()) or just check the users roles, compare the user id to updater id, etc
public function __construct(
    public $users,
    public $sendFullDetails = false
) {
//
}
public function broadcastWith(): array
{
	if ($this->sendFullDetails) {
        //full details
    } else {
        //limited details
    }
}
  • when firing the event, fire two of them
    • broadcast(new EndpointUpdated([$userA], sendFullDetails: true));
    • broadcast(new EndpointUpdated($usersB, sendFullDetails: false));

or if A/B is not enough, just loop each user and fire the event specific for him (and check his rights directly in the broadcastWith())

1 like

Please or to participate in this conversation.