10
10
11
11
class PresenceChannel extends Channel
12
12
{
13
+ /**
14
+ * Data for the users connected to this channel
15
+ *
16
+ * Note: If replication is enabled, this will only contain entries
17
+ * for the users directly connected to this server instance. Requests
18
+ * for data for all users in the channel should be routed through
19
+ * ReplicationInterface.
20
+ *
21
+ * @var string[]
22
+ */
13
23
protected $ users = [];
14
24
15
25
/**
@@ -18,21 +28,9 @@ class PresenceChannel extends Channel
18
28
*/
19
29
public function getUsers (string $ appId )
20
30
{
21
- if (config ('websockets.replication.enabled ' ) === true ) {
22
- // Get the members list from the replication backend
23
- return app (ReplicationInterface::class)
24
- ->channelMembers ($ appId , $ this ->channelName );
25
- }
26
-
27
- return $ this ->users ;
28
- }
29
-
30
- /**
31
- * @return array
32
- */
33
- public function getUserCount ()
34
- {
35
- return count ($ this ->users );
31
+ // Get the members list from the replication backend
32
+ return app (ReplicationInterface::class)
33
+ ->channelMembers ($ appId , $ this ->channelName );
36
34
}
37
35
38
36
/**
@@ -51,36 +49,27 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
51
49
$ channelData = json_decode ($ payload ->channel_data );
52
50
$ this ->users [$ connection ->socketId ] = $ channelData ;
53
51
54
- if (config ('websockets.replication.enabled ' ) === true ) {
55
- // Add the connection as a member of the channel
56
- app (ReplicationInterface::class)
57
- ->joinChannel (
58
- $ connection ->app ->id ,
59
- $ this ->channelName ,
60
- $ connection ->socketId ,
61
- json_encode ($ channelData )
62
- );
63
-
64
- // We need to pull the channel data from the replication backend,
65
- // otherwise we won't be sending the full details of the channel
66
- app (ReplicationInterface::class)
67
- ->channelMembers ($ connection ->app ->id , $ this ->channelName )
68
- ->then (function ($ users ) use ($ connection ) {
69
- // Send the success event
70
- $ connection ->send (json_encode ([
71
- 'event ' => 'pusher_internal:subscription_succeeded ' ,
72
- 'channel ' => $ this ->channelName ,
73
- 'data ' => json_encode ($ this ->getChannelData ($ users )),
74
- ]));
75
- });
76
- } else {
77
- // Send the success event
78
- $ connection ->send (json_encode ([
79
- 'event ' => 'pusher_internal:subscription_succeeded ' ,
80
- 'channel ' => $ this ->channelName ,
81
- 'data ' => json_encode ($ this ->getChannelData ($ this ->users )),
82
- ]));
83
- }
52
+ // Add the connection as a member of the channel
53
+ app (ReplicationInterface::class)
54
+ ->joinChannel (
55
+ $ connection ->app ->id ,
56
+ $ this ->channelName ,
57
+ $ connection ->socketId ,
58
+ json_encode ($ channelData )
59
+ );
60
+
61
+ // We need to pull the channel data from the replication backend,
62
+ // otherwise we won't be sending the full details of the channel
63
+ app (ReplicationInterface::class)
64
+ ->channelMembers ($ connection ->app ->id , $ this ->channelName )
65
+ ->then (function ($ users ) use ($ connection ) {
66
+ // Send the success event
67
+ $ connection ->send (json_encode ([
68
+ 'event ' => 'pusher_internal:subscription_succeeded ' ,
69
+ 'channel ' => $ this ->channelName ,
70
+ 'data ' => json_encode ($ this ->getChannelData ($ users )),
71
+ ]));
72
+ });
84
73
85
74
$ this ->broadcastToOthers ($ connection , (object ) [
86
75
'event ' => 'pusher_internal:member_added ' ,
@@ -97,15 +86,13 @@ public function unsubscribe(ConnectionInterface $connection)
97
86
return ;
98
87
}
99
88
100
- if (config ('websockets.replication.enabled ' ) === true ) {
101
- // Remove the connection as a member of the channel
102
- app (ReplicationInterface::class)
103
- ->leaveChannel (
104
- $ connection ->app ->id ,
105
- $ this ->channelName ,
106
- $ connection ->socketId
107
- );
108
- }
89
+ // Remove the connection as a member of the channel
90
+ app (ReplicationInterface::class)
91
+ ->leaveChannel (
92
+ $ connection ->app ->id ,
93
+ $ this ->channelName ,
94
+ $ connection ->socketId
95
+ );
109
96
110
97
$ this ->broadcastToOthers ($ connection , (object ) [
111
98
'event ' => 'pusher_internal:member_removed ' ,
@@ -124,19 +111,13 @@ public function unsubscribe(ConnectionInterface $connection)
124
111
*/
125
112
public function toArray (string $ appId = null )
126
113
{
127
- if (config ('websockets.replication.enabled ' ) === true ) {
128
- return app (ReplicationInterface::class)
129
- ->channelMembers ($ appId , $ this ->channelName )
130
- ->then (function ($ users ) {
131
- return array_merge (parent ::toArray (), [
132
- 'user_count ' => count ($ users ),
133
- ]);
134
- });
135
- }
136
-
137
- return array_merge (parent ::toArray (), [
138
- 'user_count ' => count ($ this ->users ),
139
- ]);
114
+ return app (ReplicationInterface::class)
115
+ ->channelMembers ($ appId , $ this ->channelName )
116
+ ->then (function ($ users ) {
117
+ return array_merge (parent ::toArray (), [
118
+ 'user_count ' => count ($ users ),
119
+ ]);
120
+ });
140
121
}
141
122
142
123
protected function getChannelData (array $ users ): array
0 commit comments