Last active
October 26, 2021 23:51
-
-
Save boskiv/393e8f3ca5155ece36d86a420d6fa59c to your computer and use it in GitHub Desktop.
Laravel Nats
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 | |
namespace App\Broadcasters; | |
use Illuminate\Broadcasting\Broadcasters\Broadcaster; | |
use Illuminate\Support\Facades\Log; | |
use Nats\Connection; | |
use Nats\ConnectionOptions; | |
class NatsBroadcaster extends Broadcaster { | |
protected string $host; | |
protected int $port; | |
public function __construct(string $host = 'localhost', int $port = 4222) | |
{ | |
$this->host = $host; | |
$this->port = $port; | |
} | |
public function auth($request) | |
{ | |
// TODO: Implement auth() method. | |
} | |
public function validAuthenticationResponse($request, $result) | |
{ | |
// TODO: Implement validAuthenticationResponse() method. | |
} | |
/** | |
* @throws \Exception | |
*/ | |
public function broadcast(array $channels, $event, array $payload = []) | |
{ | |
if (empty($channels)) { | |
return; | |
} | |
$options = new ConnectionOptions(); | |
$options->setHost($this->host); | |
$options->setPort($this->port); | |
$client = new Connection($options); | |
$client->connect(); | |
$payload['event'] = $event; | |
$payload = json_encode($payload, JSON_PRETTY_PRINT); | |
foreach ($this->formatChannels($channels) as $channel) { | |
Log::debug('Broadcasting ['.$event.'] on channel '. $channel .' with payload:' . $payload); | |
$client->publish($channel, $payload); | |
} | |
$client->close(); | |
} | |
} |
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 | |
public function boot() | |
{ | |
if (env('BROADCAST_DRIVER') == 'nats') { | |
Log::debug("Using broadcast driver nats with host:".config('broadcasting.connections.nats.host')." and port:".config('broadcasting.connections.nats.port') ); | |
Broadcast::extend('nats', function () { | |
return new NatsBroadcaster( | |
config('broadcasting.connections.nats.host'), | |
config('broadcasting.connections.nats.port') | |
); | |
}); | |
} | |
} |
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
"repositories": [ | |
{ | |
"type": "vcs", | |
"url": "https://github.com/mschindler83/phpnats" | |
} | |
], | |
"require": { | |
"mschindler83/nats": "dev-develop", | |
}, |
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 | |
'nats' => [ | |
'driver' => 'nats', | |
'host' => env('NATS_HOST'), | |
'port' => env('NATS_PORT'), | |
], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment