ejtj3 / php-nats
Nats client
Installs: 6 552
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 2
Open Issues: 2
Requires
- php: ^8.1
- ext-json: *
- nyholm/dsn: ^2.0
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-03-06 16:23:30 UTC
README
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
Setup a nats connection
<?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();
Setup a nats connection with auth
<?php declare(strict_types=1); use EJTJ3\PhpNats\Connection\NatsConnectionOption; $connectionOptions = new NatsConnectionOption('nats://admin:admin@nats-server.com:4222');
Setup a nats connection with TLS
<?php declare(strict_types=1); use EJTJ3\PhpNats\Connection\NatsConnectionOption; $connectionOptions = new NatsConnectionOption('tls://admin:admin@nats-server.com:4222');
Publish message
<?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();
Connect to a cluster
<?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);