-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht-pubsub.php
58 lines (43 loc) · 1.85 KB
/
t-pubsub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
require 'vendor/autoload.php';
use Basis\Nats\Client;
use Basis\Nats\Configuration;
function pretty ($var) {
return gettype($var) . ' ' . json_encode(
$var,
JSON_UNESCAPED_SLASHES | // Don't escape forward slashes. stripslashes() could be used afterwards instead
JSON_UNESCAPED_UNICODE | // Print unicode characters insteas of their encoding "€" vs "\u20ac"
JSON_PRETTY_PRINT | // Nice layout over several lines, human readable
JSON_PARTIAL_OUTPUT_ON_ERROR | // Substitute whatever can not be printed
JSON_INVALID_UTF8_SUBSTITUTE // Convert invalid UTF-8 characters to \0xfffd (Unicode Character 'REPLACEMENT CHARACTER')
); // Constants: https://www.php.net/manual/en/json.constants.php
}
// this is default options, you can override anyone
$configuration = new Configuration([
'host' => 'localhost',
'jwt' => null,
'lang' => 'php',
'pass' => null,
'pedantic' => false,
'port' => 4222,
'reconnect' => true,
'timeout' => 1,
'token' => null,
'user' => null,
'nkey' => null,
'verbose' => false,
'version' => 'dev',
]);
// default delay mode is constant - first retry be in 1ms, second in 1ms, third in 1ms
$configuration->setDelay(0.001);
// linear delay mode - first retry be in 1ms, second in 2ms, third in 3ms, fourth in 4ms, etc...
$configuration->setDelay(0.001, Configuration::DELAY_LINEAR);
// exponential delay mode - first retry be in 10ms, second in 100ms, third in 1s, fourth if 10 seconds, etc...
$configuration->setDelay(0.01, Configuration::DELAY_EXPONENTIAL);
// ping nats:
$client = new Client($configuration);
if (!$client->ping()) die('no ping; no nats');
$queue = $client->subscribe('stock/aex');
$client->publish('stock/aex', '09672-klm:87.95');
$message = $queue->fetch();
echo "payload: " . $message->payload . PHP_EOL;