Hey how can a store a websocket session to send a ...
# ktor
b
Hey how can a store a websocket session to send a message to the websocket later?
b
There was a thread related to this the other day https://kotlinlang.slack.com/archives/C0A974TJ9/p1606123821161400
b
but i think this thread is about the client side connection i mean the server side part
b
Ah...what I've done for that is created a channel that messages can be added to, and the server starts a coroutine which reads from that channel and sends a message whenever there is one to read
b
ok using the send method i have a problem to send a message because the method from where i want to send the message cant be a suspend fun wich is required from the send function
b
If you want your non-suspend
send
method to wait until the message is sent, you can run it within a
runBlocking
block. If you don't want to wait, you'll want to use
launch
on some
CoroutineScope
to launch a coroutine to do the send
b
what did you mean wich "some CoroutineScope"?
b
Well usually you launch a coroutine from some scope you've created.
GlobalScope
, for example, but I don't think it's recommended to use that except for specific use cases as tasks launched there can 'leak'.
Generally though I'd say you should read through some of the coroutine tutorials on the web to get a better feel for them if you're not familiar with
CoroutineScope
b
ah ok thanks for your help
b
👍
b
if i run the method its only called once. If a call it twice the second message where not send to the client
b
You'll need a loop
b
really? i have a function wich is called twice! But the message where send only once.
b
I may not be understanding what you're describing, can you paste an example?
b
can i send it via slack with a code box or via paste server?
b
You can paste it here, but I'd try and reduce it down if it's really big
b
Copy code
@EventListener
    fun onStart(e: CloudServicePreStartEvent) {
        sendNotification(SocketNotification("success", "Der Service ${e.cloudService.serviceId.name} startet"))
    }

    fun sendNotification(notification: SocketNotification) {
            webServer.notificationSockets.forEach {
                runBlocking {  it.send(Frame.Text(notification.toString())) }
            }
    }
that my code so the "CloudServicePreStartEvent" is called when a server starts. While the client was connected it was called 6 times, but the client only receive on message
b
Did you verify the
runBlocking
block in
sendNotification
was called 6 times?
b
Yes i was called 6 times
b
Ok, there might be an issue with the websocket itself, then? Maybe make sure the
send
is "finishing" each time it's called ?
b
i try to print a message before and after the send and each of then was printet in the console. So i think the send method was executed
is it possible, the error is on my javascript file?
Copy code
let socket = new WebSocket("<wss://acp.domain.com/ws>")

socket.onopen = function (event) {
    log("info", "Connected to the Notify Socket")
}

socket.onerror = function (error) {
    log("error", error.message)
}

socket.onmessage = function (m) {
    log("message", m.data)
}

function log(type, input) {
    console.log("[NOTIFY] " + type.toUpperCase() + " | " + input)
}
b
I'd break on when you're doing the send and follow it through, maybe the websocket itself is closing or something?
b
I don't think so because when closing the websocket it will be remove from the list and on next call its in the list
b
At this point I think you've just got to debug what's going on with the websocket