Search code examples
node.jskotlinsocket.iocreate-react-kotlin-app

How to use socket.io-client in KotlinJS


I want to use the socket.io-client library from nodejs in my React-Kotlin project.

I tried to load it like this:

@file:JsModule("socket.io-client")
@file:JsNonModule

package modules

@JsName("Manager")
external fun socket(uri: String): dynamic

Then client will connect to the server when I call it with this function val s = socket("http://localhost:8080/") but when I call an emit like this s.emit("testemit", mapOf("test" to false)) it doesn't send it. If I change s.emit to something else that does not exist in socket.io-client, I get an error message that the function does not exist.


Solution

  • It should work with something like this:

    val socket = kotlinext.js.require("socket.io-client")("http://localhost:8080/")
    socket.emit("testemit", json("test" to false)) { result -> println(result) }