PHP code example of ejtj3 / php-nats

1. Go to this page and download the library: Download ejtj3/php-nats library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ejtj3 / php-nats example snippets




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();




declare(strict_types=1);

use EJTJ3\PhpNats\Connection\NatsConnectionOption;

$connectionOptions = new NatsConnectionOption('nats://admin:[email protected]:4222');




declare(strict_types=1);

use EJTJ3\PhpNats\Connection\NatsConnectionOption;

$connectionOptions = new NatsConnectionOption('tls://admin:[email protected]:4222');




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();




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);