0

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.

Walter Woshid
  • 329
  • 2
  • 11
  • 1
    What information are you looking to receive from the server? 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 when the subscription is etablished – doydoy Feb 11 '21 at 12:36
  • 1
    Don't know why I look for a complicated way, when it also works like that, thanks! But my application requires other data as soon as the subscription is succeeded. I may get data from the broadcasting server, before I acquire the other data. – Walter Woshid Feb 11 '21 at 15:54
  • 1
    @doydoy Guess it works for me, could you post this as an answer so I can mark it? – Walter Woshid Feb 11 '21 at 16:22

2 Answers2

1

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.

doydoy
  • 4,021
  • 3
  • 20
  • 33
0

Edit:

.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');
    }
}
Walter Woshid
  • 329
  • 2
  • 11