PAGI
view release on metacpan or search on metacpan
examples/sse-dashboard/app.pl view on Meta::CPAN
#!/usr/bin/env perl
#
# Live Dashboard using PAGI::SSE
#
# Demonstrates real-time server metrics streaming with:
# - Automatic keepalive for proxy compatibility
# - Reconnection support via Last-Event-ID
# - Multiple event types
#
# Run: pagi-server --app examples/sse-dashboard/app.pl --port 5000
# Open: http://localhost:5000/
#
use strict;
use warnings;
use Future::AsyncAwait;
use IO::Async::Loop;
use IO::Async::Timer::Periodic;
use File::Basename qw(dirname);
use File::Spec;
use PAGI::SSE;
use PAGI::App::File;
# Shared state
my %subscribers;
my $next_id = 1;
my $event_id = 0;
my $loop;
# Metrics timer
my $metrics_timer;
sub start_metrics_broadcaster {
return if $metrics_timer;
$loop //= IO::Async::Loop->new;
$metrics_timer = IO::Async::Timer::Periodic->new(
interval => 2,
on_tick => sub {
$event_id++;
my $metrics = {
cpu => 20 + int(rand(60)),
memory => 40 + int(rand(40)),
requests => int(rand(1000)),
timestamp => time(),
};
for my $sub (values %subscribers) {
$sub->{sse}->try_send_event(
event => 'metrics',
data => $metrics,
id => $event_id,
);
}
},
);
$loop->add($metrics_timer);
$metrics_timer->start;
}
sub stop_metrics_broadcaster {
( run in 0.391 second using v1.01-cache-2.11-cpan-140bd7fdf52 )