Skip to content

Instantly share code, notes, and snippets.

@jeskew
Last active July 28, 2020 19:14
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeskew/fd07bc3e74ff8ab2345f to your computer and use it in GitHub Desktop.
Save jeskew/fd07bc3e74ff8ab2345f to your computer and use it in GitHub Desktop.
Laravel broadcasting with SNS
<?php
// standard bootstrap stuff
app('Illuminate\Broadcasting\BroadcastManager')->extend('sns', function ($app, $config) {
return new SnsBroadcaster($app['Aws\Sns\SnsClient'], [
'channel-name' => 'arn:aws:sns:us-east-1:698519295917:My-Topic'
]);
});
<?php
use Aws\Sns\SnsClient;
use Illuminate\Contracts\Broadcasting\Broadcaster;
class SnsBroadcaster implements Broadcaster
{
/** @var SnsClient */
private $sns;
/** @var array */
private $channelMapping;
public function __construct(SnsClient $client, array $channelMapping)
{
$this->sns = $client;
$this->channelMapping = $channelMapping;
}
public function broadcast(array $channels, $event, array $payload = [])
{
$payload = json_encode(['event' => $event, 'data' => $payload]);
foreach ($channels as $channel) {
$this->sns->publish([
'Message' => $payload,
'TopicArn' => array_get($this->channelMapping, $channel),
]);
}
}
}
@scottconnerly
Copy link

Its this simple?
I started looking for a package akin to https://github.com/pusher/pusher-http-php, but haven't found one that seems to be worth its salt. Maybe because this is all that's needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment