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:
CIO (Coroutine-based I/O)
In addition to the engines mentioned above, Ktor provides a special engine type
TestEngine for testing application logic. You can learn more about it from Testing.
Before using the desired engine, you need to add the corresponding dependency to your build script:
Below are examples of adding a dependency for Netty:
Choose how to create a server
A Ktor server application can be created and run in two ways: using the embeddedServer to quickly pass server parameters in code, or using EngineMain to load the configuration from the external
The embeddedServer function accepts an engine factory used to create an engine of a specific type. In the example below, we pass the Netty factory to run a server with the Netty engine and listen on the
EngineMain represents an engine for running a server. You can use the following engines:
EngineMain.main function is used to start a server with the selected engine and loads the application module specified in the external application.conf file. In the example below, we start a server from the application's
If you need to start a server using a build system task, you need to configure the required
EngineMain as the main class:
Configure an engine
In this section, we'll take a look how to specify various engine-specific options.
embeddedServer function allows you to pass engine-specific options using the
configure optional parameter. This parameter includes options common for all engines and exposed by the ApplicationEngine.Configuration class.
In addition to these options, you can configure additional engine-specific properties.
Netty-specific options are exposed by the NettyApplicationEngine.Configuration class.
CIO-specific options are exposed by the CIOApplicationEngine.Configuration class.
If you use
EngineMain, you can specify options common for all engines in the application.conf file within the