Repository navigation
Expand file tree
/
Copy path08-subscriptions-advanced.php
More file actions
153 lines (113 loc) · 3.94 KB
/
Copy path08-subscriptions-advanced.php
File metadata and controls
153 lines (113 loc) · 3.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Advanced subscription options.
*
* Demonstrates: subscribeSync with maxPending/clearMaxPending, queuedMsgs(),
* setClosedHandler, autoUnsubscribe, setPendingLimits, pending(), delivered(), dropped().
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
use Sofascore\Nats\Connection;
use Sofascore\Nats\Message;
$conn = Connection::connect();
// --- 1. Sync subscription with pending limits ---
$sub = $conn->subscribeSync('demo.pending');
// Set limits for pending messages (max messages, max bytes)
$sub->setPendingLimits(msgLimit: 100, bytesLimit: 1024 * 1024); // 100 messages, 1MB
// Publish a few messages
for ($i = 1; $i <= 5; $i++) {
$conn->publish('demo.pending', "message-{$i}");
}
$conn->flush();
// Process incoming messages to fill the buffer
$conn->process(0.5);
// Check how many messages are waiting in the buffer
echo "Queued msgs: {$sub->queuedMsgs()}\n";
$pending = $sub->pending();
echo "Pending: {$pending['messages']} messages, {$pending['bytes']} bytes\n";
// Read messages
while ($sub->queuedMsgs() > 0) {
$msg = $sub->nextMessage(1.0);
echo "Received: {$msg->data}\n";
}
echo "Delivered: {$sub->delivered()}\n";
echo "Dropped: {$sub->dropped()}\n";
// --- 2. Max pending (high-water mark) ---
// Publish more messages to track the high-water mark
for ($i = 1; $i <= 10; $i++) {
$conn->publish('demo.pending', "batch-{$i}");
}
$conn->flush();
$conn->process(0.5);
$maxPending = $sub->maxPending();
echo "Max pending (high-water): {$maxPending['messages']} messages, {$maxPending['bytes']} bytes\n";
// Reset high-water mark
$sub->clearMaxPending();
$maxPendingAfterClear = $sub->maxPending();
echo "Max pending after clear: {$maxPendingAfterClear['messages']} messages\n";
// Drain buffer
while ($sub->queuedMsgs() > 0) {
$sub->nextMessage(1.0);
}
$sub->unsubscribe();
// --- 3. AutoUnsubscribe - automatically unsubscribe after N messages ---
$autoSub = $conn->subscribeSync('demo.auto');
$autoSub->autoUnsubscribe(3); // Receive max 3 messages
for ($i = 1; $i <= 5; $i++) {
$conn->publish('demo.auto', "auto-{$i}");
}
$conn->flush();
$conn->process(0.5);
// Only the first 3 messages will be received
$count = 0;
while (true) {
try {
$msg = $autoSub->nextMessage(0.5);
$count++;
echo "Auto [{$count}]: {$msg->data}\n";
} catch (\Sofascore\Nats\TimeoutException) {
break;
}
}
echo "Auto received: {$count} messages\n";
echo "Subscription valid: " . ($autoSub->isValid() ? 'yes' : 'no') . "\n";
// --- 4. setClosedHandler - callback when subscription closes ---
$closedSub = $conn->subscribeSync('demo.closed');
$closedSub->setClosedHandler(function () {
echo "[CLOSED] Subscription 'demo.closed' has been closed!\n";
});
// Unsubscribe calls closedHandler
$closedSub->unsubscribe();
// --- 5. setClosedHandler s drain ---
$drainSub = $conn->subscribeSync('demo.drain');
$drainSub->setClosedHandler(function () {
echo "[CLOSED] Subscription 'demo.drain' closed after drain\n";
});
// Drain also calls closedHandler
$drainSub->drain();
// --- 6. Async subscription with delivered/dropped tracking ---
$delivered = 0;
$asyncSub = $conn->subscribe('demo.async', function (Message $msg) use (&$delivered) {
$delivered++;
});
for ($i = 1; $i <= 10; $i++) {
$conn->publish('demo.async', "async-{$i}");
}
$conn->flush();
$conn->process(0.5);
echo "Async delivered (internal): {$asyncSub->delivered()}\n";
echo "Async dropped: {$asyncSub->dropped()}\n";
$asyncSub->unsubscribe();
// --- 7. Queue subscription ---
$q1 = $conn->queueSubscribeSync('demo.queue', 'workers');
$q2 = $conn->queueSubscribeSync('demo.queue', 'workers');
for ($i = 1; $i <= 4; $i++) {
$conn->publish('demo.queue', "job-{$i}");
}
$conn->flush();
$conn->process(0.5);
echo "Queue sub 1 queued: {$q1->queuedMsgs()}\n";
echo "Queue sub 2 queued: {$q2->queuedMsgs()}\n";
echo "Queue sub 1 subject: {$q1->subject()}, queue: {$q1->queue()}\n";
$q1->unsubscribe();
$q2->unsubscribe();
$conn->close();