Last active
July 28, 2020 19:14
-
-
Save jeskew/fd07bc3e74ff8ab2345f to your computer and use it in GitHub Desktop.
Laravel broadcasting with SNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' | |
]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
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?