@@ -186,35 +186,32 @@ public function unsubscribeFromChannel(ConnectionInterface $connection, string $
186
186
$ this ->getGlobalConnectionsCount ($ connection ->app ->id , $ channelName )
187
187
->then (function ($ count ) use ($ connection , $ channelName ) {
188
188
if ($ count === 0 ) {
189
+ // Make sure to not stay subscribed to the PubSub topic
190
+ // if there are no connections.
189
191
$ 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 ;
200
192
}
201
193
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 );
218
215
});
219
216
220
217
parent ::unsubscribeFromChannel ($ connection , $ channelName , $ payload );
@@ -677,7 +674,7 @@ public function storeUserData($appId, string $channel = null, string $key, $data
677
674
public function removeUserData ($ appId , string $ channel = null , string $ key )
678
675
{
679
676
return $ this ->publishClient ->hdel (
680
- $ this ->getRedisKey ($ appId , $ channel ), $ key
677
+ $ this ->getRedisKey ($ appId , $ channel, [ ' users ' ] ), $ key
681
678
);
682
679
}
683
680
0 commit comments