Miguel de Orue
Miguel de Orue

Reputation: 31

Laravel Echo in multiple channels

I have a question and I do not find an answer that satisfies me.

I have an app of Soccer Teams:

I broadcast on channels this way:

new PrivateChannel('team.' . $this->team->id);

But I don't know how to register a User in multiple channels.

The question is, do I need to register the User to the channels of every team that he belongs?

for ($user->teams as $team) {
  Echo.private('team.'.$team->id) 
}

Is there any other way to register the user to his channels?

Thank you in advance.

Upvotes: 2

Views: 6285

Answers (1)

user320487
user320487

Reputation:

You'll need to authenticate the user for each team's channel in your channels.php route file:

// loop teams to create channel names like teams.team1.1, teams.team2.1
// {id} is the user's primary key
Broadcast::channel("teams.{$team->name}.{id}", function (Authenticatable $user, $id) {
    return (int)$user->getAuthIdentifier() === (int)$id;
});

// have Echo listen for broadcast events on the same channels. 
Echo.private('teams.team1.1')
    .listen('SomeTeamEvent', callback)
    .listen('AnotherTeamEvent', callback2)
    ...
    .listen('LastTeamEvent', callbackX)
...
Echo.private('teams.team2.1')
    .listen('SomeTeamEvent', anotherCallback)
    .listen('AnotherTeamEvent', anotherCallback2)
    ...
    .listen('LastTeamEvent', anotherCallback2)

Upvotes: 2

Related Questions