Skip to content

Instantly share code, notes, and snippets.

@appkr
Forked from jeskew/SnsBroadcaster.php
Created October 22, 2016 07:43
Show Gist options
  • Save appkr/0d6344aae0c0aa5785369bbfb38697b6 to your computer and use it in GitHub Desktop.
Save appkr/0d6344aae0c0aa5785369bbfb38697b6 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),
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment