Ktor 3.4: Better HTTP Client Management

Paul Krill
4 Min Read

Big news for Ktor! This Kotlin-powered framework just got an update, bringing exciting new features like ‘duplex streaming’ for faster data handling with OkHttp and the smart ability to stop unwanted HTTP requests when users disconnect.

A hand touching the word "Kotlin" on a digital display.

Hey Ktor fans! JetBrains has just rolled out Ktor 3.4, a fresh update to their awesome Kotlin framework for building both server-side and client-side apps that handle things super efficiently. This new version comes packed with goodies, including something called ‘duplex streaming’ for the OkHttp client engine and a clever new HttpRequestLifecycle plugin that lets you gracefully cancel HTTP requests when someone disconnects.

This exciting Ktor 3.4 update landed on January 23. If you’re eager to dive in, you can find all the ‘how-to’ guides over at ktor.io.

Let’s talk about that new HttpRequestLifecycle plugin! Imagine a user leaves a page while a big, complex request is still running in the background. This plugin is a lifesaver because it automatically stops (cancels) those requests when the client disconnects. It’s fantastic for preventing your server from wasting resources on requests nobody is waiting for anymore. When a client bails, the system cleverly shuts down the related processes (coroutine, launch, or async tasks) and cleans everything up neatly. Just a heads-up: right now, this smart cancellation works only with the Netty and CIO engines.

Ktor 3.4 also brings a cool new way to document your API endpoints dynamically, working hand-in-hand with a new compiler plugin. Instead of needing to generate your API documentation (like a Swagger UI) from a separate, static file, Ktor can now build this documentation on the fly, right when your application runs, pulling details straight from how your routes are defined. To get this going, developers simply enable it via the Ktor Gradle plugin, and then use the new describe API directly in their code to automatically generate those docs.

And here’s another neat feature in Ktor 3.4: the OkHttp client engine now supports ‘duplex streaming.’ What does that mean for you? It means your clients can send data *and* receive data at the same time, unlike traditional HTTP where you have to send everything before getting any response back. This makes things much more efficient! It’s specifically for HTTP/2 connections, and you can easily turn it on by setting the duplexStreamingEnabled property in your OkHttpConfig.

Finally, the Compression plugin has a new trick up its sleeve: Zstd support, available through the ktor-server-compression-zstd module. Zstd is a fantastic compression method known for being incredibly fast, achieving excellent compression, and offering customizable compression levels – all while keeping those processing times low. Great for keeping your data lean and mean!

 
 
KotlinProgramming LanguagesSoftware DevelopmentLibraries and Frameworks
 
Share This Article
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *