'How to trigger 'joining' in presence channel?
I am trying to use a presence channel to see users come and go. With this purpose, as documentation recommends, I wrote this code. And I receive this.
But how would I get the console.log for 'joining' user? If I log in as another user, I only get one user credentials. I suppose it should be an array...So what can I do to trigger 'joining' function?
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);
})
Solution 1:[1]
As far as I know, .joining() is triggered by default once at a time. Every time a new user is joining the channel, this function catch the user and dispatch a related event to the channel. Right?
When you refresh the page, the channel reloads too, triggering .leaving() and .joining() - you "left" the page and "returned" to it.
If you are the user that reloaded the page, you will not receive the "hey you..." because you're the one that is triggering the event. The ones that will receive it, are those connected to the channel.
When you rejoin the channel, after the refresh, the .here() will be triggered...
Remember: If "Echo.join()" is within the page you refreshed, you will be temporally disconnected from the channel, so you will not receive the messages that the channel is sending.
For example:
We're (you and me) connected to the same channel and I refresh the page. I'll be temporally disconneted from this channel and the channel will broadcast the content inside of .joining() to the users that are still connected to it (in this case, you).
The same is valid if you refresh the page. You'll be disconneted while I receive the message.
So, if you want to receive the message broadcasted by .joining(), make sure you're not the user that refresh the page.
:)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |