Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

0

Ktor 3.0: A Boost in Performance and Server-Sent EventsSupport

By Sergio De Simone, translated by Pingchuan, curated by DingXiaoyun

Ktor, the native Kotlin framework for building asynchronous HTTP server and client applications, has reached version 3.0. This release brings significantperformance improvements and new features, including support for server-sent events, CSRF protection, and serving static resources from ZIP files. However, it also introduces some breaking changesdue to the adoption of kotlinx-io.

Performance Enhancement through kotlinx-io

The core of Ktor 3.0’s performance boost lies in its integration with kotlinx-io, a low-level I/O librarybuilt around the concept of a Buffer. This mutable byte sequence functions like a queue, allowing data to be written to its tail and read from its head.

The breaking changes in Ktor 3 primarily affect the underlying IO API,impacting types like Input, Output, ByteReadChannel, and ByteWriteChannel. Developers directly using these types will need to adapt their applications to the new API.

The key benefit of kotlinx-io is its performance optimization. It eliminates unnecessary byte copying between ByteReadChannel, ByteWriteChannel, and the network interface, leading to more efficient byte conversion and parsing. This paves the way for future performance enhancements.

JetBrains, the creators of Ktor, have conducted benchmarks that demonstrate a significant reduction in time for file and socket operations, with improvements reaching up to 90% in some cases.

Server-Sent Events: A New Communication Channel

Beyond performance gains, Ktor 3.0 introduces a crucial feature: support for server-sent events. This server-push technology establishes a communication channel from the server to the client. Server-sent events are particularly advantageous in scenarios where data flows primarily in one direction, especially when dealing with firewall blocking or connection interruptions. Compared to WebSockets, server-sent events are more suitable in these situations. However, WebSockets offer higher efficiency and lower latency.

Additional Features in Ktor 3.0

Ktor 3.0 also includes other valuable features:

  • CSRF Protection: This feature allows specifying CSRF protection for any given route.
  • Serving Static Resources from ZIP Files: Static resources can be served directly from ZIP files, including subdirectories, which are reflected in the URL structure.

Wasm Support: Still in Alpha

It’s worth noting that Ktor 3.0 now supports Wasm as a build target for Ktor clients. However, Kotlin/Wasm is still in alpha testing, so Wasm support in Ktor 3 is not yet production-ready.

Getting Started with Ktor

To begin a new project with Ktor,visit the Ktor website and choose the most suitable starting point for your needs.

Conclusion

Ktor 3.0 represents a significant step forward for the Kotlin HTTP toolkit. Its performance improvements, support for server-sent events, and other new features make it an even more powerful and versatile framework for building asynchronousHTTP applications. While some breaking changes are present, the benefits of these updates are undeniable, paving the way for a more efficient and feature-rich development experience.


>>> Read more <<<

Views: 0

0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注