@@ -78,40 +78,45 @@ public function boot(LoopInterface $loop): ReplicationInterface
78
78
*
79
79
* @param string $redisChannel
80
80
* @param string $payload
81
- * @return bool
82
81
*/
83
82
protected function onMessage (string $ redisChannel , string $ payload )
84
83
{
85
84
$ payload = json_decode ($ payload );
86
85
87
86
// Ignore messages sent by ourselves
88
87
if (isset ($ payload ->serverId ) && $ this ->serverId === $ payload ->serverId ) {
89
- return false ;
88
+ return ;
90
89
}
91
90
92
- // We need to put the channel name in the payload
93
- $ payload ->channel = $ redisChannel ;
91
+ // Pull out the app ID. See RedisPusherBroadcaster
92
+ $ appId = $ payload ->appId ;
93
+
94
+ // We need to put the channel name in the payload.
95
+ // We strip the app ID from the channel name, websocket clients
96
+ // expect the channel name to not include the app ID.
97
+ $ payload ->channel = Str::after ($ redisChannel , "$ appId: " );
94
98
95
99
/* @var $channelManager ChannelManager */
96
100
$ channelManager = app (ChannelManager::class);
97
101
98
102
// Load the Channel instance, if any
99
- $ channel = $ channelManager ->find ($ payload ->appId , $ payload ->channel );
100
- if ($ channel === null ) {
101
- return false ;
103
+ $ channel = $ channelManager ->find ($ appId , $ payload ->channel );
104
+
105
+ // If no channel is found, none of our connections want to
106
+ // receive this message, so we ignore it.
107
+ if (! $ channel ) {
108
+ return ;
102
109
}
103
110
104
- $ socket = $ payload ->socket ;
111
+ $ socket = $ payload ->socket ?? null ;
105
112
106
- // Remove the internal keys from the payload
113
+ // Remove fields intended for internal use from the payload
107
114
unset($ payload ->socket );
108
115
unset($ payload ->serverId );
109
116
unset($ payload ->appId );
110
117
111
118
// Push the message out to connected websocket clients
112
119
$ channel ->broadcastToEveryoneExcept ($ payload , $ socket );
113
-
114
- return true ;
115
120
}
116
121
117
122
/**
@@ -123,13 +128,13 @@ protected function onMessage(string $redisChannel, string $payload)
123
128
*/
124
129
public function subscribe (string $ appId , string $ channel ): bool
125
130
{
126
- if (! isset ($ this ->subscribedChannels [$ channel ])) {
131
+ if (! isset ($ this ->subscribedChannels [" $ appId : $ channel" ])) {
127
132
// We're not subscribed to the channel yet, subscribe and set the count to 1
128
- $ this ->subscribeClient ->__call ('subscribe ' , [$ channel ]);
129
- $ this ->subscribedChannels [$ channel ] = 1 ;
133
+ $ this ->subscribeClient ->__call ('subscribe ' , [" $ appId : $ channel" ]);
134
+ $ this ->subscribedChannels [" $ appId : $ channel" ] = 1 ;
130
135
} else {
131
136
// Increment the subscribe count if we've already subscribed
132
- $ this ->subscribedChannels [$ channel ]++;
137
+ $ this ->subscribedChannels [" $ appId : $ channel" ]++;
133
138
}
134
139
135
140
return true ;
@@ -144,17 +149,17 @@ public function subscribe(string $appId, string $channel): bool
144
149
*/
145
150
public function unsubscribe (string $ appId , string $ channel ): bool
146
151
{
147
- if (! isset ($ this ->subscribedChannels [$ channel ])) {
152
+ if (! isset ($ this ->subscribedChannels [" $ appId : $ channel" ])) {
148
153
return false ;
149
154
}
150
155
151
156
// Decrement the subscription count for this channel
152
- $ this ->subscribedChannels [$ channel ]--;
157
+ $ this ->subscribedChannels [" $ appId : $ channel" ]--;
153
158
154
159
// If we no longer have subscriptions to that channel, unsubscribe
155
- if ($ this ->subscribedChannels [$ channel ] < 1 ) {
156
- $ this ->subscribeClient ->__call ('unsubscribe ' , [$ channel ]);
157
- unset($ this ->subscribedChannels [$ channel ]);
160
+ if ($ this ->subscribedChannels [" $ appId : $ channel" ] < 1 ) {
161
+ $ this ->subscribeClient ->__call ('unsubscribe ' , [" $ appId : $ channel" ]);
162
+ unset($ this ->subscribedChannels [" $ appId : $ channel" ]);
158
163
}
159
164
160
165
return true ;
@@ -173,7 +178,10 @@ public function publish(string $appId, string $channel, stdClass $payload): bool
173
178
$ payload ->appId = $ appId ;
174
179
$ payload ->serverId = $ this ->serverId ;
175
180
176
- $ this ->publishClient ->__call ('publish ' , [$ channel , json_encode ($ payload )]);
181
+ $ this ->publishClient ->__call ('publish ' , ["$ appId: $ channel " , json_encode ($ payload )]);
182
+
183
+ return true ;
184
+ }
177
185
178
186
return true ;
179
187
}
0 commit comments