avatarLeo N

Summary

The provided content discusses the use of Ktor, an asynchronous framework for building connected applications on the Android platform, highlighting its simplicity, flexibility, and support for various server engines and plugins.

Abstract

Ktor is a Kotlin framework designed for creating asynchronous client and server applications across multiple platforms, including Android. It emphasizes ease of use and flexibility, avoiding any kind of 'magic' in its design. Ktor supports a variety of server engines such as Netty, Jetty, Tomcat, and CIO, and allows for custom engine creation. The framework operates on a request/response pipeline that utilizes plugins to process requests and responses, providing a routing mechanism to direct requests to the appropriate handlers. Ktor is unopinionated, allowing developers to choose their technologies for logging, templating, and more, and it leverages Kotlin coroutines for asynchronous programming. The content also demonstrates how to set up a simple Ktor server within an Android application and concludes with an invitation to explore and contribute to the example code on GitHub.

Opinions

  • Ktor aims to be a simple and straightforward framework for building connected applications, emphasizing the importance of asynchronous programming for a better user experience.
  • The goal of Ktor is to provide an end-to-end multiplatform application framework, with current support for JVM client and server scenarios, as well as clients for JavaScript, iOS, and Android.
  • Ktor's design philosophy discourages 'magic' in application development, preferring clear and understandable code.
  • The framework's flexibility is showcased through its support for multiple server engines and the ability to run applications in various servlet containers or standalone with Netty or Jetty.
  • Ktor's plugin system is highlighted as a key feature that allows for customizable request/response pipelines, enabling developers to tailor the application logic to their needs.
  • The content suggests that Ktor's unopinionated nature and support for application composition with functions or classes, with or without dependency injection, provides developers with the freedom to structure their applications as they see fit.
  • The use of Kotlin coroutines in Ktor is presented as a modern approach to asynchronous programming that avoids thread blocking and simplifies the development of non-blocking applications.
  • Testability is emphasized as a principle of Ktor, with the framework providing a test environment that simplifies integration testing without the need for extensive mocking.
  • The conclusion encourages readers to engage with the Ktor community by forking the example code on GitHub and invites feedback on the article, indicating a community-driven approach to development and knowledge sharing.

Android: Embedded Server with Ktor

Create asynchronous client and server applications. Anything from microservices to multiplatform HTTP client apps in a simple way. Open Source, free, and fun!

1. Overview

  • Ktor is a framework to easily build connected applications — web applications, HTTP services, mobile and browser applications. Modern connected applications need to be asynchronous to provide the best experience to users, and Kotlin coroutines provide awesome facilities to do it in an easy and straightforward way.
  • While not yet entirely there, the goal of Ktor is to provide an end-to-end multiplatform application framework for connected applications. Currently, JVM client and server scenarios are supported, as well as JavaScript, iOS, and Android clients, and we are working on bringing server facilities to native environments, and client facilities to other native targets.
  • Ktor can be used to create a variety of server and client-side applications. Whether we want to create a website that serves static and dynamic pages, an HTTP endpoint, a RESTful system, or even microservices, Ktor makes it all possible.

2. Setup

3. Build the Server, Routing

One of Ktor’s goals is to remain as simple as possible and to avoid any kind of magic. The equivalent to a Hello World application in Ktor would be:

We start by creating a server, in this case using Netty as an Engine, and setting the port to 5001. After this, we define a route to respond to requests made to / with a simple Hello, world! text. Finally, we start the server.

  • The embedded Server function is a simple way to configure server parameters in code and quickly run an application.
  • EngineMain provides more flexibility to configure a server. You can specify server parameters in an application.conf file and change a configuration without recompiling your application. Moreover, you can run your application from a command line and override the required server parameters by passing corresponding command-line arguments.
  • Runs embedded web server on localhost:5001
  • Installs routing and responds with Hello, world! when receiving a GET HTTP request for the root path

4. Engines

To run a Ktor server application, you need to create and configure a server first. Server configuration can include different settings: a server engine, various engine-specific options, host and port values, and so on. The following engines are supported:

  • Netty
implementation "io.ktor:ktor-server-netty:$ktor_version"
  • Jetty
implementation "io.ktor:ktor-server-jetty:$ktor_version"
  • Tomcat
implementation "io.ktor:ktor-server-tomcat:$ktor_version"
  • CIO (Coroutine-base I/O)
implementation "io.ktor:ktor-server-cio:$ktor_version"

Those are the official engines developed for Ktor, but it is also possible to create your own engines and provide custom configurations for them.

5. Plugins

A typical request/response pipeline in Ktor looks like the following:

https://ktor.io/docs/plugins.html

It starts with a request which is routed to a specific handler, processed by our application logic, and finally responded to.

As a request comes in:

  • It is routed to the correct handler via the routing mechanism
  • Before being handed off to the handler, it goes through one or more Plugins
  • The handler (application logic) handles the request
  • Before the response is sent to the client, it goes through one or more Plugins

6. Running Server

7. Principles

Unopinionated

Ktor Framework doesn’t impose a lot of constraints on what technology a project is going to use — logging, templating, messaging, persistence, serialization, dependency injection, etc. Sometimes it may be required to implement a simple interface, but usually, it is a matter of writing a transforming or intercepting function. Features are installed into the application using a unified interception mechanism that allows building arbitrary pipelines.

Ktor Applications can be hosted in any servlet container with Servlet 3.0+ API support such as Tomcat, or standalone using Netty or Jetty. Support for other hosts can be added through the unified hosting API.

Ktor APIs are mostly function calls with lambdas. Thanks to Kotlin DSL capabilities, the code looks declarative. Application composition is entirely up to the developer’s choice — with functions or classes, using dependency injection framework, or doing it all manually in the main function.

Asynchronous

The Ktor pipeline machinery and API are utilizing Kotlin coroutines to provide easy-to-use asynchronous programming model without making it too cumbersome. All host implementations are using asynchronous I/O facilities to avoid thread blocking.

Testable

Ktor applications can be hosted in a special test environment, which emulates a web server to some extent without actually doing any networking. It provides an easy way to test an application without mocking too much stuff, and still achieve good performance while validating application calls. Running integration tests with a real embedded web server is of course possible, too.

8. Conclusion

In this article, we are built a small server application inside a small android app in a few minutes. As always, you can fork the code over on Githubm please feel free to comment on this article

Android
Ktor
Kotlin
Server
Embedded Systems
Recommended from ReadMedium