| 1 | package PAGI::FastAPI::Response::SSE; | ||||||||||
| 2 | |||||||||||
| 3 | 24 | ▶ | use v5.38; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 24 | ▶ | use experimental 'class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 24 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 24 | our $VERSION = qv('v1.7.1'); | |||||||||
| 8 | 24 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||
| 9 | |||||||||||
| 10 | 24 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 24 | ▶ | use PAGI::SSE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 24 | class PAGI::FastAPI::Response::SSE { | |||||||||
| 14 | 24 | field $generator :param; | |||||||||
| 15 | 24 | field $headers :param = []; | |||||||||
| 16 | 24 | field $status :param = 200; | |||||||||
| 17 | |||||||||||
| 18 | 3 | ▶ | async method dispatch ($scope, $receive, $send) { | ||||||||
|
Subroutine
dispatch: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 19 | 3 | my $sse = PAGI::SSE->new($scope, $receive, $send); | |||||||||
| 20 | |||||||||||
| 21 | await $sse->start( | ||||||||||
| 22 | status => $status, | ||||||||||
| 23 | headers => [ | ||||||||||
| 24 | ['x-accel-buffering' => 'no'], | ||||||||||
| 25 | 3 | @{$headers}, | |||||||||
| 26 | ], | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 3 | eval { | |||||||||
| 30 | 3 | await $generator->($sse); | |||||||||
| 31 | }; | ||||||||||
| 32 | 3 | ▶ | if (my $err = $@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | await $sse->_trigger_error($err); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 3 | ▶ | await $sse->run if !$sse->is_closed; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 24 | } | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | =encoding utf-8 | ||||||||||
| 41 | |||||||||||
| 42 | =head1 NAME | ||||||||||
| 43 | |||||||||||
| 44 | PAGI::FastAPI::Response::SSE - Server-Sent Events (SSE) Streaming Response for PAGI::FastAPI | ||||||||||
| 45 | |||||||||||
| 46 | =head1 VERSION | ||||||||||
| 47 | |||||||||||
| 48 | Version v1.7.1 | ||||||||||
| 49 | |||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||
| 51 | |||||||||||
| 52 | use PAGI::FastAPI; | ||||||||||
| 53 | |||||||||||
| 54 | my $app = PAGI::FastAPI->new(); | ||||||||||
| 55 | |||||||||||
| 56 | $app->get('/api/v1/notifications', sub ($c) { | ||||||||||
| 57 | return $c->sse(async sub ($sse) { | ||||||||||
| 58 | # Enable keepalive comments every 20 seconds | ||||||||||
| 59 | await $sse->keepalive(20); | ||||||||||
| 60 | |||||||||||
| 61 | # Replay missed messages on client reconnection | ||||||||||
| 62 | if (my $last_id = $sse->last_event_id) { | ||||||||||
| 63 | my @missed = get_events_since($last_id); | ||||||||||
| 64 | for my $event (@missed) { | ||||||||||
| 65 | await $sse->send_event(%$event); | ||||||||||
| 66 | } | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # Send a structured SSE event | ||||||||||
| 70 | await $sse->send_event( | ||||||||||
| 71 | event => 'notification', | ||||||||||
| 72 | id => 'evt-101', | ||||||||||
| 73 | data => { user => 'alice', status => 'online' }, | ||||||||||
| 74 | ); | ||||||||||
| 75 | |||||||||||
| 76 | # Stream plain JSON messages | ||||||||||
| 77 | await $sse->send_json({ message => 'System operational' }); | ||||||||||
| 78 | |||||||||||
| 79 | # Close stream when done | ||||||||||
| 80 | await $sse->close(reason => 'complete'); | ||||||||||
| 81 | }); | ||||||||||
| 82 | }); | ||||||||||
| 83 | |||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||
| 85 | |||||||||||
| 86 | C<PAGI::FastAPI::Response::SSE> provides first-class, non-blocking | ||||||||||
| 87 | Server-Sent Events (SSE) streaming capabilities for L<PAGI::FastAPI> | ||||||||||
| 88 | applications by wrapping L<PAGI::SSE>. | ||||||||||
| 89 | |||||||||||
| 90 | It automatically manages connection lifecycles, flow control/backpressure, | ||||||||||
| 91 | heartbeats, custom headers (such as disabling Nginx buffering via | ||||||||||
| 92 | C<X-Accel-Buffering: no>), and structured event formatting (JSON auto-encoding, | ||||||||||
| 93 | event IDs, retry intervals, and keepalive comments). | ||||||||||
| 94 | |||||||||||
| 95 | =head1 CONSTRUCTOR | ||||||||||
| 96 | |||||||||||
| 97 | =head2 C<new(%options)> | ||||||||||
| 98 | |||||||||||
| 99 | Instantiates a new SSE response object. Accepts the following named parameters: | ||||||||||
| 100 | |||||||||||
| 101 | =over 4 | ||||||||||
| 102 | |||||||||||
| 103 | =item * C<generator> (Required) | ||||||||||
| 104 | |||||||||||
| 105 | An C<async sub ($sse)> reference containing the streaming loop or event | ||||||||||
| 106 | emitter. Receives a L<PAGI::SSE> instance as its sole argument. | ||||||||||
| 107 | |||||||||||
| 108 | =item * C<headers> (Optional) | ||||||||||
| 109 | |||||||||||
| 110 | An ArrayRef of header key-value pairs (e.g., C<< [ ['X-Custom-Header' => 'value'] ] >>) | ||||||||||
| 111 | to return with the initial HTTP response stream. Defaults to C<[]>. | ||||||||||
| 112 | |||||||||||
| 113 | =item * C<status> (Optional) | ||||||||||
| 114 | |||||||||||
| 115 | Integer HTTP status code for the initial handshake response. Defaults to C<200>. | ||||||||||
| 116 | |||||||||||
| 117 | =back | ||||||||||
| 118 | |||||||||||
| 119 | =head1 METHODS | ||||||||||
| 120 | |||||||||||
| 121 | =head2 C<dispatch($scope, $receive, $send)> | ||||||||||
| 122 | |||||||||||
| 123 | await $response->dispatch($scope, $receive, $send); | ||||||||||
| 124 | |||||||||||
| 125 | Executes the SSE response lifecycle against the low-level PAGI connection. | ||||||||||
| 126 | This method initialises L<PAGI::SSE>, issues the initial C<sse.start> event | ||||||||||
| 127 | with default and custom HTTP headers, runs the C<generator> callback, and | ||||||||||
| 128 | waits on C<$sse->run> until client disconnect or stream closure. | ||||||||||
| 129 | |||||||||||
| 130 | =head1 SEE ALSO | ||||||||||
| 131 | |||||||||||
| 132 | L<PAGI::SSE>, L<PAGI::FastAPI::Context>, L<PAGI::FastAPI> | ||||||||||
| 133 | |||||||||||
| 134 | =head1 AUTHOR | ||||||||||
| 135 | |||||||||||
| 136 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||
| 137 | |||||||||||
| 138 | =head1 REPOSITORY | ||||||||||
| 139 | |||||||||||
| 140 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||
| 141 | |||||||||||
| 142 | =head1 BUGS | ||||||||||
| 143 | |||||||||||
| 144 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||
| 145 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||
| 146 | bug as I make changes. | ||||||||||
| 147 | |||||||||||
| 148 | =head1 SUPPORT | ||||||||||
| 149 | |||||||||||
| 150 | You can find documentation for this module with the perldoc command. | ||||||||||
| 151 | |||||||||||
| 152 | perldoc PAGI::FastAPI::Response::SSE | ||||||||||
| 153 | |||||||||||
| 154 | You can also look for information at: | ||||||||||
| 155 | |||||||||||
| 156 | =over 4 | ||||||||||
| 157 | |||||||||||
| 158 | =item * BUG Report | ||||||||||
| 159 | |||||||||||
| 160 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||
| 161 | |||||||||||
| 162 | =item * Search MetaCPAN | ||||||||||
| 163 | |||||||||||
| 164 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||
| 165 | |||||||||||
| 166 | =back | ||||||||||
| 167 | |||||||||||
| 168 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 169 | |||||||||||
| 170 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||
| 171 | |||||||||||
| 172 | This program is free software; you can redistribute it and/or modify it under | ||||||||||
| 173 | the terms of the Artistic License (2.0). | ||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||
| 176 | |||||||||||
| 177 | 24 | 1; # End of PAGI::FastAPI::Response::SSE | |||||||||