Search code examples
perlwww-mechanizelwp-useragentevent-stream

Fetch text/event-stream web response using WWW::Mechanize or LWP::UserAgent


I'm using WWW::Mechanize to fetch a web page that includes a Google Maps widget that receives constant data from a single response of type text/event-stream.

That kind of response is like a never ending response from the server that constantly returns updated data for the widget to work.

I'm trying to find out how to read that exact response from Perl. Using something like:

my $mech = WWW::Mechanize->new;

# Do some normal GET and POST requests to authenticate and set cookies for the session

# Now try to get that text/event-stream response

$mech->get('https://some.domain.com/event_stream_page');

But that doesn't work because the response never ends.

How can I make that request and start reading the response and do something with that data every time the server updates the stream?


Solution

  • Found a way to do this. Using a handler from LWP, from which WWW::Mechanize inherits:

    $mech->add_handler (
        'response_data',
        sub {
            my ($response, $ua, $h, $data) = @_;
            # Your chunk of response is now in $data, do what you need
            # If you plan on reading an infinite stream, it's a good idea to clean the response so it doesn't grow infinitely too!
            $response->content(undef);
            # Important to return a true value if you want to keep reading the response!
            return 1;
        },
    );