-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime-example.php
More file actions
157 lines (123 loc) · 4.33 KB
/
realtime-example.php
File metadata and controls
157 lines (123 loc) · 4.33 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
154
155
156
157
<?php
declare(strict_types=1);
/**
* Realtime events example for the Spooled PHP SDK.
*
* This demonstrates subscribing to real-time job and queue events
* using Server-Sent Events (SSE) or WebSocket.
*/
require_once __DIR__ . '/../vendor/autoload.php';
use Spooled\SpooledClient;
use Spooled\Config\ClientOptions;
use Spooled\Realtime\SseClient;
use Spooled\Realtime\WebSocketClient;
// Create client
$client = new SpooledClient(new ClientOptions(
apiKey: getenv('API_KEY') ?: 'your-api-key',
baseUrl: getenv('BASE_URL') ?: 'https://api.spooled.cloud',
));
echo "=== Spooled Realtime Events Example ===\n\n";
// Method 1: Use the unified realtime client (via SpooledClient)
echo "Method 1: Unified realtime client\n";
echo str_repeat('-', 40) . "\n";
$realtime = $client->realtime();
// Check what transport is available
if ($realtime->isWebSocketAvailable()) {
echo "WebSocket transport available (preferred)\n";
} else {
echo "Using SSE transport (WebSocket not available)\n";
}
// Method 2: Direct SSE client (always available)
echo "\nMethod 2: Direct SSE Client\n";
echo str_repeat('-', 40) . "\n";
$sse = new SseClient(
baseUrl: getenv('BASE_URL') ?: 'https://api.spooled.cloud',
apiKey: getenv('API_KEY') ?: 'your-api-key',
);
// Subscribe to all events
$sse->subscribe(function (array $event): void {
$type = $event['type'] ?? 'unknown';
$data = $event['data'] ?? [];
echo "[{$type}] ";
if (is_array($data)) {
$jobId = $data['jobId'] ?? $data['job_id'] ?? 'N/A';
$queue = $data['queueName'] ?? $data['queue_name'] ?? 'N/A';
echo "Job: {$jobId}, Queue: {$queue}";
} else {
echo json_encode($data);
}
echo "\n";
});
// Subscribe to specific event types
$sse->on('job.created', function (array $event): void {
echo "🆕 New job created!\n";
});
$sse->on('job.completed', function (array $event): void {
echo "✅ Job completed!\n";
});
$sse->on('job.failed', function (array $event): void {
echo "❌ Job failed!\n";
});
// Subscribe to a specific queue
$sse->subscribeToQueue('my-queue', function (array $event): void {
echo "📬 Event on my-queue: {$event['type']}\n";
});
// Subscribe to a specific job
// $sse->subscribeToJob('job-id-here', function (array $event): void {
// echo "📌 Event for specific job: {$event['type']}\n";
// });
// Connection lifecycle events
$sse->on('connected', function (array $data): void {
echo "🔗 Connected to SSE stream\n";
});
$sse->on('reconnecting', function (array $data): void {
$delay = $data['delay'] ?? 0;
$attempt = $data['attempt'] ?? 0;
echo "🔄 Reconnecting (attempt {$attempt}) in {$delay}ms...\n";
});
// Handle shutdown gracefully
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, fn () => $sse->stop());
pcntl_signal(SIGINT, fn () => $sse->stop());
echo "\nPress Ctrl+C to stop listening.\n\n";
}
// Method 3: WebSocket client (if available)
if (WebSocketClient::isAvailable()) {
echo "\nMethod 3: WebSocket Client (available)\n";
echo str_repeat('-', 40) . "\n";
$ws = new WebSocketClient(
wsUrl: str_replace('http', 'ws', getenv('BASE_URL') ?: 'wss://api.spooled.cloud') . '/api/v1/ws',
apiKey: getenv('API_KEY') ?: 'your-api-key',
);
// WebSocket allows bidirectional communication
$ws->on('message', function (array $event): void {
echo "[WS] {$event['type']}: " . json_encode($event['data'] ?? []) . "\n";
});
// WebSocket-specific subscriptions
$ws->subscribeToQueue('my-queue');
// $ws->subscribeToJob('job-id');
// Note: Don't start both SSE and WS - pick one
echo "WebSocket client configured (not starting - using SSE for this demo)\n";
}
// Create a test job to see events
echo "\nCreating a test job to trigger events...\n";
try {
$testJob = $client->jobs->create([
'queue' => 'realtime-test',
'payload' => ['test' => true, 'timestamp' => time()],
]);
echo "Created test job: {$testJob->id}\n";
echo "Watch for events below:\n\n";
} catch (\Throwable $e) {
echo "Could not create test job: {$e->getMessage()}\n";
}
// Start listening (blocking - will run until stopped)
echo "Listening for events (Ctrl+C to stop)...\n\n";
try {
$sse->listen();
} catch (\Throwable $e) {
echo "SSE error: {$e->getMessage()}\n";
}
// Cleanup
$client->close();
echo "\nDone!\n";