Laravel Echo in multiple channels - laravel

I have a question and I do not find an answer that satisfies me.
I have an app of Soccer Teams:
A user can join multiple teams,
I want to send notifications when an action in a team occurs (for example, when a match is scheduled)
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.

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)

Related

Laravel echo is not listening to dynamically created one-to-one private channel

SO i am trying to listen to event that create channel dynamically with the the ids of the two users involved. I ma using pusher and echo for this purpose
the event successfully fired from my controller and is being recorded but echo does not listens to that event.
I am using guards as the conversation will be between two admins
My channel.php code is
Broadcast::channel('chatchannel.{reciever_id}.{sender_id}', function ($user, $reciever_id, $sender_id) {
if((int) auth()->guard('admin')->user()->id === (int) $reciever_id || (int) auth()->guard('admin')->user()->id === (int) $sender_id){
return true;
}else{
return false;
}
});
app.js file looks like this
Echo.private('chatchannel.'+app_sender_id+'.'+app_reciever_id)
.listen('.chatEvent', (e) => {
console.log('subscribed');
});
I did this change in my broadcastingerviceprovider.php file according to online soultions but it did not work
Broadcast::routes(['middleware' => 'auth:admin']);
I looked for all the solutions online but could not find anything that is of actual help. Can anyone guide me on how to get it working.
Your broadcast channel is chatchannel.{reciever_id}.{sender_id}.
However your client channel is chatchannel.'+app_sender_id+'.'+app_reciever_id
This means, for a sender of A and receiver of 2 you would have the following channels:
Broadcast - chatchannel.2.A
Client - private-chatchannel.A.2.
Channel names must match for the client to receive the broadcast event. You should ensure the sender and receiver Id are in the same order on both systems and that you are using private channels in both scenarios.

Handling logout on private channel Laravel 9 Websockets

I am making a Laravel 9 application involving websockets. I've made a private channel in the 'routes/channels.php' as following:
Broadcast::channel('User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
It succesfully only allows authenticated users to join the channel and receive events. But the problem is, if I open two browser tabs, and logout at one, the other one is still 'authenticated' in the channel and is still able to receive new events. I want to make sure that if the user logs out of their account, he does not have access to the private channel anymore. How would this be possible?

How does Laravel Model Broadcasting deal with private channel auth? And some other questions about broadcasting in Laravel

I'm working on a simple chat web app using Laravel.
I've decided to use the Model broadcasting routine here.
But I don't know how model broadcasting authorize users.
I know the standard method is like:
Broadcast::channel('orders.{orderId}', function ($user, $orderId) {
return $user->id === Order::findOrNew($orderId)->user_id;
});
While model broadcasting also broadcasts on private channels, the docs doesn't describe if developers need to set up authorization callbacks.
So, do I need to create a private channel authorization callback like:
Broadcast::channel('App.User.{userId}', function ($user, $userId) {
return $user->id === userId;
});
And the second question: can I create different private channels authorization callbacks for different auth guards like:
Broadcast::channel('channel.{memberId}', function ($member, $memberId) {
// ...
}, ['guards' => ['member']]);
Broadcast::channel('channel.{adminId}', function ($admin, $adminId) {
// ...
}, ['guards' => ['admin']]);
The last question: does the broadcasting in Laravel support client-to-client broadcasting? Because I need to limit who can send messages to specific channels, if the client-to-client behavior exists it may effect the privacy of users in my app.
My native language is not English. Sorry if my words are not clear.

Laravel Echo How to take an action when the users joins a channel?

Using Laravel 5.5, vue.js and laravel echo.
I want to apply an instruction that something happens whenever the user, that is attempting to join the channel, has a successful join.
In my case, if it was a successful join it will add an object to an array.
Echo.join('chat' + e.thread.id)
.listen('ThreadPosted', (e) => {
console.log("ThreadPosted");
});
How can this be achieved?
You need to call the method "here" :
Echo.join('channel')
.here((users) => {
// client has joined successfully
})
.listen('Event', callback );
As mentioned in Laravel echo documentation :
The "here" callback will be executed immediately once the channel is joined successfully, and will receive an array containing the user information for all of the other users currently subscribed to the channel.
There are also two other callbacks available, "joining" and "leaving"
The "joining" method will be executed when a new user joins a channel, while the "leaving" method will be executed when a user leaves the channel.
Echo.join('channel')
.joining((user) => {
// a user has joined.
})
.leaving((user) => {
// a user has left.
});
Bear in mind that all these methods are available for Presence Channels, which is mostly used for chat applications.

Parse Cloud Code to send push notification to group of other users

I have an app where one user can invite other users to join an event by push notification. Let's say when creating an event, the user add other users to this event, then save the event to Parse.
So basically I have an array of user_id and I will call a function from cloud code to push notification to those Id, after saving the event.
1)Will the following Cloud code work?
Parse.Cloud.afterSave( "Event", function(request) {
//Get value from Ticket Object
var ids = request.object.get("inviteeIds");
//Set push query
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.containedIn("objectId",ids);
//Send Push message
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Event Added",
sound: "default"
}
},{
success: function(){
response.success('true');
},
error: function (error) {
response.error(error);
}
});
});
I am not sure if the containedIn function exist or not:
pushQuery.containedIn("objectId",ids);
When I search I only find documentation about equalTo function, e.g:
query.equalTo('injuryReports', true);
2) I also read about Channel, but I still not understand how to apply it in my situation. From the documentation:
Devices start by subscribing to one or more channels, and
notifications can later be sent to these subscribers.
In my case how can I create a Channel and then add ids of friends who I want to invite to this Channel?
If possible, I would like to use Cloud Code rather than pushing from mobile device.
1)Will the following Cloud code work?
Why don't you try it and see for yourself, then come back with the errors, if any? Anyway, there's no response in afterSave. It will return a success regardless of what happens in it.
Otherwise it may work. Try running it.
I am not sure if the containedIn function exist or not:
Parse.Query.containedIn
2) I also read about Channel, but I still not understand how to apply it in my situation
Basically you subscribe to a particular channel in the client. Like this (Android)
ParsePush.subscribeInBackground("channelName");
Then in the Cloud
Parse.Push.send({
channels: channelList,
data: {
// etc
}
});
Obviously you'll need to know the channels you want to target.
You can subscribe multiple users to the same channel (for example you can have a dedicated channel for a particular event) or you can have one channel per user (for example you can name it something like channel_<userId> and only subscribe that user to it). Up to you what you need or what you want.
One last thing...
So basically I have an array of user_id
Keep in mind that objects stored in the database have a limited size. If your object gets too big and has too much data, you won't be able to add any more to it.

Resources