Nats publisher
This is a simple package to publish messages to Nats
Installation
You can install the package using the Composer package manager. You can install it by running this command in your project root:
composer require ejtj3/php-nats
TODO
- Autodiscovery
- TLS authentication
- Auto reconnect
Setup a nats connection
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
use EJTJ3\PhpNats\Connection\NatsConnection;
$connectionOptions = new NatsConnectionOption('nats://nats-server.com:4222');
$connection = new NatsConnection($connectionOptions);
// connect to nats-server
$connection->connect();
// send ping and wait for the 'PONG' response
$connection->validatePing();
// close connection
$connection->close();
Setup a nats connection with auth
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
$connectionOptions = new NatsConnectionOption('nats://admin:[email protected]:4222');
Setup a nats connection with TLS
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
$connectionOptions = new NatsConnectionOption('tls://admin:[email protected]:4222');
Publish message
declare(strict_types=1);
// connect to nats-server
$connection->connect();
$subject = 'hello';
$payload = 'world';
// publish world to the hello subject
$connection->publish($subject, $payload);
// close connection
$connection->close();
Connect to a cluster
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
use EJTJ3\PhpNats\Connection\NatsConnection;
$connectionOptions = new NatsConnectionOption('nats://admin:[email protected]:4222,nats://admin:[email protected]:4222,nats://admin:[email protected]:4222');
____________________OR________________________
$connectionOptions = new NatsConnectionOption([
'nats://admin:[email protected]:4222',
'nats://admin:[email protected]:4222',
'nats://admin:[email protected]:4222',
]);
$connection = new NatsConnection($connectionOptions);