Brad_Fresh
Brad_Fresh

Reputation: 119

How to fix the user info doesn't shows up on the page

I am following the Presence Channels section in Laravel docs. 1.Authorizing Presence Channels-I created I function to check is user is authorized to access them.

Broadcast::channel('chat', function ($user) {
        ...
  return user_info;
   })

2.Joining Presence Channels-They say I must use Echo's join method. So I did.

Echo.join('chat')
   .here((users) => {
       console.log('hello',users)
       this.users = users;
   })
   .joining((user) => {
       console.log('hey you', user)
       this.users.push(user);
   })
   .leaving((user) => {
       this.users.splice(this.users.indexOf(user), 1);
   })

Here's the part that confuses me. "The data returned by the authorization callback will be made available to the presence channel event listeners in your JavaScript application". I assume that I suppose to have this Javascript. part and it should be an event listener. I just can't understand where should it be and how I must call it. Have it something to do with a function I use when user logged in? So, help me understand how to implement these 'presence channel event listeners in your JavaScript application.'

Upvotes: 0

Views: 53

Answers (1)

matticustard
matticustard

Reputation: 5149

"The data returned by the authorization callback will be made available to the presence channel event listeners in your JavaScript application."
https://laravel.com/docs/5.8/broadcasting#authorizing-presence-channels

This means that the data returned by your authorization callback Broadcast::channel(...) which is $user_info will be available to the joining() and leaving() listeners, or any custom listeners, within your JavaScript application.

The currently defined listeners are waiting to hear another user join or leave the chat channel. Therefore, each user must also fire the corresponding events within their own instance of the application.

// join the channel — trigger joining()
Echo.join('chat');
// leave the channel — trigger leaving()
Echo.leave('chat');

Upvotes: 1

Related Questions