ktor-native-worker-tutorial

Ktor Native Worker Tutorial

Welcome to the Ktor Native Worker Tutorial! This comprehensive guide will teach you how to build a production-ready Kotlin Multiplatform backend using Ktor, targeting both JVM and Native platforms (Linux, macOS, Windows).

What You’ll Build

A backend service that:

Architecture

HTTP Request → Ktor Server → RabbitMQ → Worker → FCM

The application demonstrates:

Tutorial Parts

Part 1: Gradle Setup

Learn how to configure a Kotlin Multiplatform project with Ktor:

Topics: Gradle, Kotlin Multiplatform, Project Structure


Part 2: Sending Notifications

Implement Firebase Cloud Messaging notification sending:

Topics: FCM, Flareon, Service Pattern, Multiplatform APIs


Part 3: AMQP Setup and Calls

Set up RabbitMQ message broker for asynchronous processing:

Topics: RabbitMQ, AMQP, Message Queues, Worker Pattern


Part 4: Routes

Create type-safe HTTP endpoints with Ktor:

Topics: Ktor Routing, HTTP APIs, Type Safety


Part 5: Wire Everything with Koin

Wire all components together using dependency injection:

Topics: Dependency Injection, Koin, Configuration Management


Part 6: Test and Demo

Run, test, and deploy the complete application:

Topics: Testing, Deployment, Performance, Production


Prerequisites

Before starting this tutorial, you should have:

Technologies Used

Technology Version Purpose
Kotlin 2.2.21 Programming language
Ktor 3.3.3 HTTP server framework
Koin 4.1.0 Dependency injection
Kourier AMQP 0.4.0 RabbitMQ client
Flareon 0.1.1 FCM messaging
Kotlinx Serialization - JSON handling

Quick Start

  1. Clone the repository
  2. Set up RabbitMQ (Docker recommended)
  3. Add Firebase service account JSON
  4. Run the application:
    ./gradlew jvmRun
    
  5. Test with curl:
    curl -X POST http://localhost:8080/api/notifications \
      -H "Content-Type: application/json" \
      -d '{"token": "xxx", "title": "Hello", "body": "Test"}'
    

Learning Path

Recommended order:

  1. Start with Part 1: Gradle Setup to understand the project structure
  2. Continue through parts 2-5 to learn each component
  3. Finish with Part 6: Test and Demo to run the complete system

Alternative paths:

Key Concepts

Throughout this tutorial, you’ll learn:

Why Native?

Native compilation with Kotlin offers:

Project Structure

ktor-native-worker-tutorial/
├── build.gradle.kts              # Build configuration
├── settings.gradle.kts           # Project settings
├── gradle.properties             # Gradle properties
├── gradle/
│   └── libs.versions.toml        # Version catalog
├── src/
│   ├── commonMain/kotlin/        # Shared code
│   │   ├── Application.kt        # Main entry point
│   │   ├── config/               # Configuration
│   │   ├── di/                   # Dependency injection
│   │   ├── messaging/            # Message broker
│   │   ├── routes/               # HTTP routes
│   │   └── services/             # Business logic
│   ├── jvmMain/kotlin/           # JVM-specific code
│   └── nativeMain/kotlin/        # Native-specific code
└── docs/                         # This tutorial!

Getting Help

If you encounter issues:

  1. Check the Test and Demo troubleshooting section
  2. Review the relevant tutorial part
  3. Examine the actual code in the src/ directory
  4. Ensure all prerequisites are met (RabbitMQ, Firebase, etc.)

Next Steps

Ready to begin? Start with Part 1: Gradle Setup!


Note: All tutorials are based on the actual project code and follow clean code conventions. No fictional code or examples - everything you see can be found in the repository.