Reputation: 349
How do I receive data when the client initially joins a channel?
Something like this:
Echo.channel('channel-name')
.onjoin(function (data) {
console.log(data) // Data received from server
})
As soon when the client joins, the server should respond with data, preferably with PHP.
Upvotes: 0
Views: 3372
Reputation: 4091
Presence channels return information about the users in the channel at the time the client subscribes.
If you require other data from your server then you could send a post request to your server to get the information. You could invoke this in a pusher:subscription_succeeded
binding so it occurs as soon as the subscription is established.
Upvotes: 1
Reputation: 349
.here will display the data per user connected, which is not what I want.
I thought presence channel was what I was looking for. https://laravel.com/docs/8.x/broadcasting#presence-channels
// Define channel
Broadcast::channel('channel-name', function () {
if (/* Condition if user is allowed to join channel */) {
return ['data' => 'your Data'];
}
});
// Join channel
Echo.join('channel-name')
.here(function(data) {
console.log(data)
})
// Event
class Event implements ShouldBroadcast
{
public function broadcastOn()
{
return new PresenceChannel('channel-name');
}
}
Upvotes: 0