Ways of rendering a stream

25 views
Skip to first unread message

David Cook

unread,
Oct 23, 2020, 5:13:37 AM10/23/20
to Mojolicious
Hi all,

I've read through all the documentation that I can find, and I'm having trouble understanding how Mojolicious renders streams. 

I have seen https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Streaming, but is the drain callback really the only solution? It's a very awkward construct, and while it is streaming out data, I think there's an issue with the callback I'm using, which I'll have to investigate another time...

I've read the documentation, but I can't get https://docs.mojolicious.org/Mojolicious/Controller#write or https://docs.mojolicious.org/Mojolicious/Controller#write_chunk to work apart from the aforementioned drain callback, even though the documentation says "Calling this method without a chunk of data will finalize the response headers and allow for dynamic content to be written later."

Typically, with a stream, you'd just write the HTTP headers, and then just start writing out your content. I don't understand why it's so challenging with Mojolicious. I figured finalizing the response headers would be what I needed, but now I have no idea.  

I'm not trying to do anything fancy. I'm just trying to read records from a database, and write them out to in a stream to the browser. I would think many people would have this same use case without doing anything too complicated in their web framework.

Anyway, hoping that someone is able to point me in the right direction. 


Sebastian Riedel

unread,
Oct 23, 2020, 5:15:51 AM10/23/20
to Mojolicious
Typically, with a stream, you'd just write the HTTP headers, and then just start writing out your content. I don't understand why it's so challenging with Mojolicious. I figured finalizing the response headers would be what I needed, but now I have no idea.  

I'm not trying to do anything fancy. I'm just trying to read records from a database, and write them out to in a stream to the browser. I would think many people would have this same use case without doing anything too complicated in their web framework.

The drain callback is the only way, because we use an event loop and non-blocking I/O. The whole thing could theoretically be wrapped in promises and then made to look blocking with async/await, but that doesn't really change how it works internally.

--
sebastian 

dc...@prosentient.com.au

unread,
Oct 25, 2020, 8:54:27 PM10/25/20
to mojol...@googlegroups.com

Thanks for your reply, Sebastian.

 

After I sent my email, I figured that the drain callback must be the only way. I just had to brush off the Node.js memories and then it all made enough sense. I think it was just confusing because a lot of Mojolicious code can be written in a direct style without needing to use continuation passing, and the documentation didn’t quite make the drain event requirement clear to me, although it all makes more sense in hindsight.

 

We’re in a slow process of trying to move the 20 year old Koha project away from CGI, so originally I was going to do a bare bones PSGI handler, but we’re starting to use Mojolicious for new parts of the project, so I thought I’d try that instead. While the continuation passing style might not be intuitive, it’ll be much nicer to have an asynchronous Mojolicious handler, so that one request for a large stream of data doesn’t totally tie up the backend server processes. I’m excited to play with Mojolicious more.

 

And thanks for mentioning the async/await. That looks like a much more user-friendly alternative, although I think that we’ll wait until Mojo::Base doesn’t flag it as experimental. We also have a long tail of older Perl versions to support, unfortunately.

 

Cheers,

 

David Cook

Software Engineer

Prosentient Systems

72/330 Wattle St

Ultimo, NSW 2007

Australia

 

Office: 02 9212 0899

Online: 02 8005 0595

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/c4412264-2954-4f5b-a0ed-45f506d53df4n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages