Skip to content

Commit 3404185

Browse files
committed
update pusher deps and update broadcasting
1 parent 4e40d4e commit 3404185

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"phpunit/phpunit": "Required to use assertions and run tests (^8.5.8|^9.3.3).",
145145
"predis/predis": "Required to use the predis connector (^1.1.2).",
146146
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
147-
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0).",
147+
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0|^5.0).",
148148
"symfony/cache": "Required to PSR-6 cache bridge (^5.1.4).",
149149
"symfony/filesystem": "Required to enable support for relative symbolic links (^5.1.4).",
150150
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0).",

src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Broadcasting\BroadcastException;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Str;
8+
use Pusher\ApiErrorException;
89
use Pusher\Pusher;
910
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1011

@@ -110,20 +111,44 @@ public function broadcast(array $channels, $event, array $payload = [])
110111
{
111112
$socket = Arr::pull($payload, 'socket');
112113

113-
$response = $this->pusher->trigger(
114-
$this->formatChannels($channels), $event, $payload, $socket, true
115-
);
114+
if ($this->pusherServerIsVersionFiveOrGreater()) {
115+
$parameters = $socket !== null ? ['socket_id' => $socket] : [];
116+
117+
try {
118+
$this->pusher->trigger(
119+
$this->formatChannels($channels), $event, $payload, $parameters
120+
);
121+
} catch (ApiErrorException $e) {
122+
throw new BroadcastException(
123+
sprintf('Pusher error: %s.', $e->getMessage())
124+
);
125+
}
126+
} else {
127+
$response = $this->pusher->trigger(
128+
$this->formatChannels($channels), $event, $payload, $socket, true
129+
);
130+
131+
if ((is_array($response) && $response['status'] >= 200 && $response['status'] <= 299)
132+
|| $response === true) {
133+
return;
134+
}
116135

117-
if ((is_array($response) && $response['status'] >= 200 && $response['status'] <= 299)
118-
|| $response === true) {
119-
return;
136+
throw new BroadcastException(
137+
! empty($response['body'])
138+
? sprintf('Pusher error: %s.', $response['body'])
139+
: 'Failed to connect to Pusher.'
140+
);
120141
}
142+
}
121143

122-
throw new BroadcastException(
123-
! empty($response['body'])
124-
? sprintf('Pusher error: %s.', $response['body'])
125-
: 'Failed to connect to Pusher.'
126-
);
144+
/**
145+
* Determine if the Pusher PHP server is version 5.0 or greater.
146+
*
147+
* @return bool
148+
*/
149+
protected function pusherServerIsVersionFiveOrGreater()
150+
{
151+
return class_exists(ApiErrorException::class);
127152
}
128153

129154
/**

0 commit comments

Comments
 (0)