Learning Ktor — Quick and Dirty Scratch Notes

Photo by Mohammad Rahmani on Unsplash

I started studying Ktor as an introduction to learning server-side code, due to my familiarity with Kotlin. In order to not pigeonhole myself in the world of Kotlin though, I have since switched to Node.js as my backend language of choice. That, and the fact that Node.js has by far, more extensive use through-out the land.

Below are my scratch notes from my short, but sweet, study of Ktor. Due to that, they could easily be inaccurate or incorrect in someway. Feel free to leave a comment letting me know if such.

Here is the project I created following Ktor’s own Creating HTTP API tutorial.

Files

application.conf

  • Details for the server — with what specifics to create the server.
  • The server’s config file.
  • Port, middleware, etc.

application.kt

  • Initializes Netty (the engine).
  • This is the middleware that processes the incoming requests.
  • Declares the various routes you set-up

Study Notes

group

  • In build.gradle.kts
  • It defines the namespace of the produced artifact. Since we plan on deploying this to a server, it needs a namespace.
  • As oppose to in Android, where the namespace is set by the Manifest.

Gradle’s Application plugin

  • Allows you to execute a JVM executable app.
  • Makes it easy to do so locally during development.

Ktor VS Netty (Language VS Framework)

  • It’s like Kotlin and Android. Ktor is the language (Kotlin), while Netty is the framework (Android).
  • A framework is a set of commonly used tools, ready for use — you don’t have to set them up every time you want to start a new project (e.g., RxJava).
  • Ktor/Kotlin, is the language you used to write the logic.
  • While Netty/Android, are the tools you need to use to actually do stuff (send/receive network calls, display GUI, etc.).
  • A framework invokes your code, unlike a library where you invoke the library. The framework executes your logic — inverse to a library, where your code executes the library.
  • Another way is like a car. The car itself is the framework (Netty), while you are the logic (Ktor).
  • Or like a game engine, your custom logic needs something that will run it. Your custom physics logic operates within a physic’s engine, not on its own. The physic engine drives the physics of you shooting the box, the custom logic the dev writes specifies how the devs wants the box to react. Like I said about frameworks being an inverse library, this physic engine calls your custom logic to know exactly how it should handle the bullet hitting the box. The engine dictates how the logic should be written — not the other way around.

Diagram: framework and language

  • Take this diagram.
  • The request comes in from the client, the initial acceptance of that request (e.g., authentication, the nitty gritty of how that all works, etc.) is handled by the framework.
  • The framework then passes off the parsed request to your custom logic.
  • The framework finally takes care of sending your formatted response back to the client.

ContentNegotiation

  • The client specifies what type of content (text, PDF, etc.) is best suited for the client.
  • So, for my simple program, it will be JSON.

--

--

--

I’m a Java and Android app developer with a passion for writing programs that are clean, testable, and modular.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is Flutter? The Game Changer in Application Development

Freshworks + Swedbyte: Growing Together on the Freshworks Platform

Dumb Elixir VIsual (and iMproved) editor

MySQL Beginners Series — The SELECT statement

Flask Introduction for API Development with Python and Jinja Templates

Intro to Modern Web Development

Django Best Practices: Refactoring Django Fat Models

Build It Yourself

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
David Precopia

David Precopia

I’m a Java and Android app developer with a passion for writing programs that are clean, testable, and modular.

More from Medium

Volatile in Java

Test case failed only when maven build

compileOnly and runtimeOnly, My experiment with them.

Callback in Java