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 )