@@ -186,35 +186,32 @@ public function unsubscribeFromChannel(ConnectionInterface $connection, string $
186186 $ this ->getGlobalConnectionsCount ($ connection ->app ->id , $ channelName )
187187 ->then (function ($ count ) use ($ connection , $ channelName ) {
188188 if ($ count === 0 ) {
189+ // Make sure to not stay subscribed to the PubSub topic
190+ // if there are no connections.
189191 $ this ->unsubscribeFromTopic ($ connection ->app ->id , $ channelName );
190-
191- $ this ->removeUserData (
192- $ connection ->app ->id , $ channelName , $ connection ->socketId
193- );
194-
195- $ this ->removeChannelFromSet ($ connection ->app ->id , $ channelName );
196-
197- $ this ->removeConnectionFromSet ($ connection );
198-
199- return ;
200192 }
201193
202- $ this ->decrementSubscriptionsCount (
203- $ connection ->app ->id , $ channelName ,
204- )
205- ->then (function ($ count ) use ($ connection , $ channelName ) {
206- if ($ count < 1 ) {
207- $ this ->unsubscribeFromTopic ($ connection ->app ->id , $ channelName );
208-
209- $ this ->removeUserData (
210- $ connection ->app ->id , $ channelName , $ connection ->socketId
211- );
212-
213- $ this ->removeChannelFromSet ($ connection ->app ->id , $ channelName );
214-
215- $ this ->removeConnectionFromSet ($ connection );
216- }
217- });
194+ $ this ->decrementSubscriptionsCount ($ connection ->app ->id , $ channelName )
195+ ->then (function ($ count ) use ($ connection , $ channelName ) {
196+ // If the total connections count gets to 0 after unsubscribe,
197+ // try again to check & unsubscribe from the PubSub topic if needed.
198+ if ($ count < 1 ) {
199+ $ this ->unsubscribeFromTopic ($ connection ->app ->id , $ channelName );
200+ }
201+ });
202+
203+ $ this ->getChannelMember ($ connection , $ channelName )
204+ ->then (function ($ member ) use ($ connection , $ channelName ) {
205+ if ($ member ) {
206+ $ this ->userLeftPresenceChannel (
207+ $ connection , json_decode ($ member ), $ channelName ,
208+ );
209+ }
210+ });
211+
212+ $ this ->removeChannelFromSet ($ connection ->app ->id , $ channelName );
213+
214+ $ this ->removeConnectionFromSet ($ connection );
218215 });
219216
220217 parent ::unsubscribeFromChannel ($ connection , $ channelName , $ payload );
@@ -677,7 +674,7 @@ public function storeUserData($appId, string $channel = null, string $key, $data
677674 public function removeUserData ($ appId , string $ channel = null , string $ key )
678675 {
679676 return $ this ->publishClient ->hdel (
680- $ this ->getRedisKey ($ appId , $ channel ), $ key
677+ $ this ->getRedisKey ($ appId , $ channel, [ ' users ' ] ), $ key
681678 );
682679 }
683680
0 commit comments