https://kotlinlang.org logo
#ktor
Title
# ktor
j

Joost Klitsie

07/12/2021, 8:23 PM
Hello guys! Is it possible for modules to communicate with one another?
a

Aleksei Tirman [JB]

07/13/2021, 9:12 AM
What do you mean by communication of modules?
j

Joost Klitsie

07/14/2021, 3:13 PM
like if I want to build my feature modules with separate features, but I want these features to call upon each other. So lets say I have a module A and module B, I would like, if both features are "installed", that A can communicate with the classes from module B without making a backend call from A to B (as they live in the same system)
a

Aleksei Tirman [JB]

07/15/2021, 9:10 AM
Since features are installed globally and there is no difference in which module a feature resides you can get an instance of any feature and just call its methods. Here is an example where
MyFeature2
calls a method of
MyFeature1
:
Copy code
fun main(args: Array<String>) {
    embeddedServer(Netty, port = 8082, watchPaths = emptyList()) {
        install(MyFeature1)
        install(MyFeature2)
    }.start()
}

class MyFeature1 {
    fun communicate() {
        println("Communicating...")
    }

    companion object: ApplicationFeature<Application, Unit, MyFeature1> {
        override val key: AttributeKey<MyFeature1> = AttributeKey("MyFeature1")

        override fun install(pipeline: Application, configure: Unit.() -> Unit): MyFeature1 {
            return MyFeature1()
        }
    }
}

class MyFeature2 {
    companion object: ApplicationFeature<Application, Unit, MyFeature2> {
        override val key: AttributeKey<MyFeature2> = AttributeKey("MyFeature1")

        override fun install(pipeline: Application, configure: Unit.() -> Unit): MyFeature2 {
            val feature1 = pipeline.feature(MyFeature1)
            feature1.communicate()
            return MyFeature2()
        }
    }
}
j

Joost Klitsie

07/19/2021, 9:22 AM
But is it possible to do this with modules? (instead of features)
a

Aleksei Tirman [JB]

07/19/2021, 12:04 PM
No
👍 1
j

Joost Klitsie

07/21/2021, 7:31 AM
that is very clear 😄
8 Views