PHP code example of repejota / nats
1. Go to this page and download the library: Download repejota/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' );
repejota / nats example snippets
$client = new \Nats\Connection();
$client->connect();
$client->subscribe(
'foo' ,
function ($message) {
printf("Data: %s\r\n" , $message->getBody());
}
);
$client->publish('foo' , 'Marty McFly' );
$client->wait(1 );
$sid = $client->subscribe(
'sayhello' ,
function ($message) {
$message->reply('Reply: Hello, ' .$message->getBody().' !!!' );
}
);
$client->request(
'sayhello' ,
'Marty McFly' ,
function ($message) {
echo $message->getBody();
}
);
$encoder = new \Nats\Encoders\JSONEncoder();
$options = new \Nats\ConnectionOptions();
$client = new \Nats\EncodedConnection($options, $encoder);
$client->connect();
$client->subscribe(
'foo' ,
function ($payload) {
printf("Data: %s\r\n" , $payload->getBody()[1 ]);
}
);
$client->publish(
'foo' ,
[
'Marty' ,
'McFly' ,
]
);
$client->wait(1 );
$sid = $client->subscribe(
'sayhello' ,
function ($message) {
$message->reply('Reply: Hello, ' .$message->getBody()[1 ].' !!!' );
}
);
$client->request(
'sayhello' ,
[
'Marty' ,
'McFly' ,
],
function ($message) {
echo $message->getBody();
}
);
php composer.phar install