Hello guys! Is it possible for modules to communic...
# ktor
j
Hello guys! Is it possible for modules to communicate with one another?
a
What do you mean by communication of modules?
j
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
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
But is it possible to do this with modules? (instead of features)
a
No
👍 1
j
that is very clear 😄