As I was playing with Pusher and WebSockets, I realized that there was one thing I can not do with Pusher : dealing with user status when a new user join a channel.
## The problem
I was coding a small messaging app, with user connection to a private channel, and managing user statuses between online, away and offline. All was fine until I connect user Anatol, mark him as away, and connect user Paul. From Paul’s point of view, Anatol was… online Hu, Oh…
## The reason
When joining a channel, Pusher return a list of users connected to this channel, but all it could tell us is “User Paul is connected, user Anatol is connected”. Argh, I want to know if Anatol is online or away when I connect as Paul. So how can we do that ?
## The solution
The only way I’ve find, confirmed by the Pusher support team (thanks to us for validating this solution), is to store user status in database through REST.
In practice, let’s see how to do this:
So, what have we done here ?
Anatol connects to the app,
Anatol goes away, trough Pusher we trigger this change to inform all the connected users,
We Post to database this change (0->1 for status),
Paul connects to the app,
We Get Anatol status (0 == ‘away’), and change is state to away in the user list,
When Anatol get back, is status is updated through Pusher.
Did you know another way to do this ? Feel free to share.
We use own and third party cookies to improve our services and show related advertising to your preferences by analyzing your browsing habits. If you continue to browse, consider accepting its use