Giter VIP home page Giter VIP logo

ktor-plugin-mqtt's Introduction

LINE

KTOR - MQTT Plugin

Introduction

This plugin provides a full MQTT client solution for KTOR servers. It is based on Paho Java by Eclipse.

Documentation

Compatibility

  • The v1.1.0 works with KTOR 1.6.5
  • The v2.0.0 works with KTOR 2.1.3

Methods available :

  • Every Paho method

Provided by the lib, available everywhere under Mqtt.client.

Mqtt.client.publishMessageTo(topic: Topic, message: String, qos: QualityOfService, retained: Boolean)
Mqtt.client.unsubscribeFrom(topic: Topic)
Mqtt.client.connectToBroker()
Mqtt.client.shutdown()

Listen to topics

โš ๏ธ You need the Routing plugin from ktor

routing {
    topic(topic: Topic, QualityOfService) {
        // some stuff, MQTT message is available within the "it" variable
    }
}

Examples

Installation example that auto-connect to the broker on localhost and subscribes to the "microbit" topic

/plugins/Mqtt.kt

package com.example.plugins

import com.example.utils.mqtt.*
import io.ktor.server.application.*
import io.ktor.server.routing.*

fun Application.configureMqtt() {
    // Example topic
    val microbit: Topic = Topic("microbit")
    val microbitTopicSubscription: TopicSubscription = TopicSubscription(microbit, AtMostOnce)

    // Installs the plugin to the server so that you can use it, won't work otherwise
    install(Mqtt) {
        broker = "tcp://localhost:1883"
        autoConnect = true
        initialSubscriptions(microbitTopicSubscription)
    }

    // Allows to map function to different topics
    routing {
        topic("microbit", AtMostOnce) {
            val message = it.toString()
            println(message)
        }
    }
}

/Application.kt

package com.example

import com.example.plugins.configureHTTP
import com.example.plugins.configureRouting
import com.example.plugins.configureSecurity
import com.example.plugins.configureSerialization
import com.example.plugins.configureMqtt
import io.ktor.server.application.*

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

fun Application.module() {
    configureSecurity()
    configureHTTP()
    configureSerialization()
    configureMqtt()
    configureRouting()
}

Publish message example

package com.example.test

import io.traxterz.ktor.mqtt.AtMostOnce
import io.traxter.ktor.mqtt.Mqtt
import io.traxter.ktor.mqtt.Topic

class ExampleClass {
    suspend fun sendMessage() {
        val microbit = Topic("microbit")
        Mqtt.client.publishMessageTo(microbit, "TEST", AtMostOnce, false)
    }
}

Contributors

ktor-plugin-mqtt's People

Contributors

dennisschroeder avatar julien-cpsn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

crowforkotlin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.