Question

I'm using Perl Dancer in a project and I want to implement SSEs http://www.html5rocks.com/en/tutorials/eventsource/basics/#toc-introduction-differences

I have a Dancer route that i'm trying to keep alive

get '/stream' => sub{
    my $response = Dancer::SharedData->response;
    debug($response->exists);
    $response->status(200);
    $response->content("data: cool test\n\n");
    $response->content_type( 'text/event-stream' );
    $response->header( 'Cache-Control' => 'no-cache' );
    $response->header( 'Connection' => 'Keep-Alive' );
    $response->pass;
    return undef;
};

It appears that returning anything from a dancer route closes the connection. Ideally i'd like to keep it open and store the$response to push more data to later.

Update: on further research it looks like this should be possible with PSGI which dancer uses: http://search.cpan.org/~miyagawa/PSGI-1.03/PSGI.pod#Delayed_Reponse_and_Streaming_Body Looking into a middleware approach at this time.

Was it helpful?

Solution

The answer here was moving to a different webserver. I was using starman which dosen't hold http connections. Move to mojolicious or twiggy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top