Skip to content

Commit

Permalink
update pusher deps and update broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 22, 2021
1 parent 4e40d4e commit 3404185
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"phpunit/phpunit": "Required to use assertions and run tests (^8.5.8|^9.3.3).",
"predis/predis": "Required to use the predis connector (^1.1.2).",
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0|^5.0).",
"symfony/cache": "Required to PSR-6 cache bridge (^5.1.4).",
"symfony/filesystem": "Required to enable support for relative symbolic links (^5.1.4).",
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0).",
Expand Down
47 changes: 36 additions & 11 deletions src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Broadcasting\BroadcastException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Pusher\ApiErrorException;
use Pusher\Pusher;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

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

$response = $this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $socket, true
);
if ($this->pusherServerIsVersionFiveOrGreater()) {
$parameters = $socket !== null ? ['socket_id' => $socket] : [];

try {
$this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $parameters
);
} catch (ApiErrorException $e) {
throw new BroadcastException(
sprintf('Pusher error: %s.', $e->getMessage())
);
}
} else {
$response = $this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $socket, true
);

if ((is_array($response) && $response['status'] >= 200 && $response['status'] <= 299)
|| $response === true) {
return;
}

if ((is_array($response) && $response['status'] >= 200 && $response['status'] <= 299)
|| $response === true) {
return;
throw new BroadcastException(
! empty($response['body'])
? sprintf('Pusher error: %s.', $response['body'])
: 'Failed to connect to Pusher.'
);
}
}

throw new BroadcastException(
! empty($response['body'])
? sprintf('Pusher error: %s.', $response['body'])
: 'Failed to connect to Pusher.'
);
/**
* Determine if the Pusher PHP server is version 5.0 or greater.
*
* @return bool
*/
protected function pusherServerIsVersionFiveOrGreater()
{
return class_exists(ApiErrorException::class);
}

/**
Expand Down

0 comments on commit 3404185

Please sign in to comment.