IO-Async-Pg
view release on metacpan or search on metacpan
examples/08-live-dashboard/README.md view on Meta::CPAN
# 08 - Live Dashboard
Real-time metrics display using pub/sub.
## What it shows
- Metrics recording with instant notifications
- Live dashboard updates via LISTEN/NOTIFY
- Pattern for real-time data streaming
## Architecture
```
âââââââââââââââââââ NOTIFY âââââââââââââââââââ
â Metrics Producerâ ââââââââââââââââââââââââââ Dashboard â
â â â â
â record_metric() â â subscribe() â
â INSERT + NOTIFY â â display() â
ââââââââââ¬âââââââââ ââââââââââ¬âââââââââ
â â
â â
â¼ â¼
âââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PostgreSQL â
â metrics table + LISTEN/NOTIFY channels â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââ
```
## Pattern
**Producer (records metrics):**
```perl
async sub record_metric {
my ($name, $value) = @_;
my $conn = await $pg->connection;
await $conn->query('INSERT INTO metrics ...', $name, $value);
await $conn->query("NOTIFY metrics, '$payload'");
$conn->release;
}
```
**Dashboard (receives updates):**
```perl
my $pubsub = $pg->pubsub;
$pubsub->subscribe('metrics', sub {
my ($channel, $payload) = @_;
update_display($payload);
})->get;
# Run event loop to receive notifications
$loop->run;
```
## Real-World Extensions
This pattern extends naturally to:
### Web Dashboard
```perl
# In a web server (e.g., Mojolicious)
$pubsub->subscribe('metrics', sub {
my ($channel, $payload) = @_;
# Push to all connected WebSocket clients
for my $client (@websocket_clients) {
$client->send($payload);
}
( run in 1.312 second using v1.01-cache-2.11-cpan-140bd7fdf52 )