Server engines
To run a Ktor server application, you need to create and configure a server first. Server configuration includes different settings:
An engine for processing network requests.
Host and port values used to access a server.
SSL settings.
Supported platforms
The table below lists the platforms supported by each engine:
Engine | Platforms | HTTP/2 |
|---|---|---|
| JVM | ✅ |
| JVM | ✅ |
| JVM | ✅ |
| ✖️ | |
JVM | ✅ |
Add dependencies
Before using the desired engine, you need to add the corresponding dependency to your build script:
ktor-server-nettyktor-server-jetty-jakartaktor-server-tomcat-jakartaktor-server-cio
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
embeddedServerto quickly pass server parameters in codeUsing
EngineMainto load configuration from an external application.conf or application.yaml file.
embeddedServer
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 8080 port:
EngineMain
EngineMain represents an engine for running a server. You can use the following engines:
io.ktor.server.netty.EngineMainio.ktor.server.jetty.jakarta.EngineMainio.ktor.server.tomcat.jakarta.EngineMainio.ktor.server.cio.EngineMain
Creating and starting the server
The EngineMain.main() function is used to start a server with the selected engine and loads the application module specified in the external configuration file. In the example below, the application's main function starts a server:
If you need to start a server using a build system task, you need to configure the required EngineMain as the main class:
Creating the server instance without starting it
In addition to directly invoking EngineMain.main() to start the server immediately, you can instead call EngineMain.createServer() which returns an EmbeddedServer instance without starting it.
This approach gives you control over when to call .start(), .stop(), or perform any operations with the server before it begins accepting requests.
Configure an engine
In this section, we'll take a look at how to specify various engine-specific options.
In code
The embeddedServer function allows you to pass engine-specific options using the configure parameter. This parameter includes options common for all engines and exposed by the ApplicationEngine.Configuration class.
The example below shows how to configure a server using the Netty engine. Within the configure block, we define a connector to specify the host and port, and customize various server parameters:
The connectors.add() method defines a connector with the specified host (127.0.0.1) and port (8080).
In addition to these options, you can configure other engine-specific properties.
Netty
Netty-specific options are exposed by the NettyApplicationEngine.Configuration class.
Jetty
Jetty-specific options are exposed by the JettyApplicationEngineBase.Configuration class.
You can configure the Jetty server inside the configureServer block, which provides access to a Server instance.
Use the idleTimeout property to specify the duration of time a connection can be idle before it gets closed.
CIO
CIO-specific options are exposed by the CIOApplicationEngine.Configuration class.
Tomcat
If you use Tomcat as the engine, you can configure it using the configureTomcat property, which provides access to a Tomcat instance.
In a configuration file
If you use EngineMain, you can specify options common for all engines within the ktor.deployment group.
Netty
You can also configure Netty-specific options in a configuration file within the ktor.deployment group: