BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin HTTP Toolkit Ktor 3.0 Improves Performance and Adds Support for Server-Sent Events

Kotlin HTTP Toolkit Ktor 3.0 Improves Performance and Adds Support for Server-Sent Events

This item in japanese

Ktor, Kotlin's native framework to create asynchronous HTTP server and client applications, has reached version 3. It adopts kotlinx-io, which brings improved performance albeit at the cost of breaking changes, and adds support for Server-Sent events, CSFR, serving static resources from ZIP files, and more.

kotlinx-io is a low-level I/O library built around the abstraction of Buffer, which is a mutable sequence of bytes. Buffers work like a queue, meaning you write data to its tail and read it from its head. Ktor 3 now aliases kotlinx.io.Source to implement its Input type, deprecates Output, and reimplements ByteReadChannel and ByteWriteChannel. Developers that use these low-level classes directly will need to modify their apps to migrate to the new API.

The main benefit brought by kotlinx-io is improved performance:

we’ve cut down on the unnecessary copying of bytes between ByteReadChannel, ByteWriteChannel, and network interfaces. This allows for more efficient byte transformations and parsing, making room for future performance improvements.

Based on its own benchmark, JetBrains say the new Ktor shows a significant reduction in the time required for file and socket operations, which can be as high as 90% in certain cases.

Besides performance, the most significant change in Ktor 3.0 is support for server-sent events, a server-push technology that enables the creation of server-to-client communication channels. Server-sent events are preferable to WebSockets in scenarios where data flows mostly in one direction, especially when it is necessary to circumvent firewall blocking or deal with connection drops. WebSockets, on the other hand, are more efficient and grant lower latency.

Other useful features in Ktor 3.0 are support for Cross-Site Request Forgery (CSRF), which can be specified for any given route, and the ability to serve static resources directly from ZIP archives. ZIP archives are served from a base path and may include subdirectories, which will be reflected in the URL structure appropriately.

As a final note about Ktor 3, it is worth noting that the Ktor client now supports Wasm as a build target. However, Kotlin/Wasm is still in the alpha stage, so Wasm support in Ktor 3 is not ready yet for production use.

To start a new project using Ktor, head to the Ktor website and choose the plugins that most suit your requirements. Available plugins provide authentication, routing, monitoring, serialization, and more. If you want to update your existing Ktor 2 project to use Ktor 3, make sure you read the migration guide provided by JetBrains.

About the Author

Rate this Article

Adoption
Style

BT