This is a simple package to publish messages to Nats
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
<?php
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();
<?php
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
$connectionOptions = new NatsConnectionOption('nats://admin:admin@nats-server.com:4222');
<?php
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
$connectionOptions = new NatsConnectionOption('tls://admin:admin@nats-server.com:4222');
<?php
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();
<?php
declare(strict_types=1);
use EJTJ3\PhpNats\Connection\NatsConnectionOption;
use EJTJ3\PhpNats\Connection\NatsConnection;
$connectionOptions = new NatsConnectionOption('nats://admin:admin@nats-server0.com:4222,nats://admin:admin@nats-server1.com:4222,nats://admin:admin@nats-server2.com:4222');
____________________OR________________________
$connectionOptions = new NatsConnectionOption([
'nats://admin:admin@nats-server0.com:4222',
'nats://admin:admin@nats-server1.com:4222',
'nats://admin:admin@nats-server2.com:4222',
]);
$connection = new NatsConnection($connectionOptions);