Mojolicious 4.0 is coming soon!

As a newer member of the Mojolicious Core Development Team, I am more than usually excited for a Mojolicious release. This is because the next major release, version 4.0, is set to ship very soon! For those of you who don’t know, Mojolicious is a modern Perl web framework which is lightweight and easy to get started learning and using, while containing features that are cutting-edge. It’s asynchronous/non-blocking to the core, websockets work out of the box, comes with built-in DOM/JSON/UserAgent, etc etc.

Our fearless leader Sebastian Riedel (aka sri) will no doubt post a message with all the details when it ships. In the meantime, I want to share a little story of how community interaction, even at the StackOverflow level, can lead to innovation and enhancement of major projects like Mojolicious!

On with the story! StackOverflow helped me learn Perl several years ago. On occasion, to pay-it-forward, I do my best to help a (polite, courteous) questioner. I hope that I can use that platform to keep a prospective Perl user in the Perl world by suggesting good tutorials or books, suggesting good practices and yes explain some of Perl’s lovable quirks. Sometimes I even get to promote some of my favorite Perl modules and projects too.

My favorites though are the rare times when the question is interesting, or better still, when a question makes a real impact. The other day someone asked a seemigly simple question: using Mojolicious’ built-in event loop (which will use EV/AnyEvent if available), how can one restart a timer event. This might be useful for pushing back a timeout for example.

I posted my naive response, in which the “restart” was really stopping the old timer and starting it again. Because I was interested, I asked sri what his opinions on my solution were. Based on his response I added a second example which the timer is kept alive, but now it has to repeatedly check if it should fire. The discussion didn’t end there, however, but lasted several days, eventually leading to implementing a new feature in Mojolicious. The benefit is not just an easy way to restart a timer, but a massive performance enhancement for Mojolicious apps under high-load with high-concurrency.

With these new optimizations, sri’s example app can maintain 10_000 concurrent websocket connection with zero CPU overhead! That’s some really awesome results to obtain when starting from a humble little StackOverflow question. But indeed it’s these cases that make me love open source software even more! Communities of people asking questions and giving answers, communities of experienced programmers and newbies too can all work together to make what’s already awesome even more awesome!

My personal favorite new feature is that sending JSON over websockets, something my Galileo CMS does regularly, will be easier than ever. To send JSON from the backend to the browser, simply use the send method as always, but with the new json key

$ws->send({ json => { foo => [qw/ bar baz bat /] } });

and when JSON is expected, you can get it simply by subscribing to the new json event

$ws->on( json => sub { my ($ws, $data) = @_; ...  } );

This all makes JSON over websocket a really easy way to do complex client/server interaction and makes doing it a breeze!

I’m looking forward to Mojolicious 4.0! I hope you are too!

Leave a comment

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.