Repository navigation
Expand file tree
/
Copy path02-watch.php
More file actions
57 lines (41 loc) · 1.47 KB
/
Copy path02-watch.php
File metadata and controls
57 lines (41 loc) · 1.47 KB
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
/**
* Key-Value Watch - change tracking with a generator-based watcher.
*
* Watcher tracks changes on keys and yields KeyValueEntry objects.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
use Sofascore\Nats\Connection;
use Sofascore\Nats\KeyValue\KeyValueConfig;
use Sofascore\Nats\KeyValue\WatchOptions;
$conn = Connection::connect();
$js = $conn->jetStream();
$kv = $js->createKeyValue(new KeyValueConfig(
bucket: 'config',
history: 10,
));
// --- Watch all keys ---
$watcher = $kv->watch(
'>', // All keys
WatchOptions::updatesOnly(), // Only new changes (not initial)
WatchOptions::ignoreDeletes(), // Ignore deletions
);
// In another process or loop iteration someone would do put/delete...
// $kv->put('db.host', 'localhost');
// $kv->put('db.port', '5432');
// watch() returns an iterator-style watcher.
// Iteration blocks while waiting for updates; there is no background worker here.
// foreach ($watcher as $entry) {
// echo "Changed: {$entry->key} = {$entry->value} (rev {$entry->revision})\n";
// if ($shouldStop) {
// $watcher->stop();
// }
// }
// --- Watch specific pattern ---
// $watcher = $kv->watch('db.*');
// --- Watch with history ---
// $watcher = $kv->watch('>', WatchOptions::includeHistory());
// --- Watch with resume from revision ---
// $watcher = $kv->watch('>', WatchOptions::resumeFromRevision(42));
$js->deleteKeyValue('config');
$conn->close();