in ktor websockets, am I expected to manage which ...
# ktor
f
in ktor websockets, am I expected to manage which client I am talking to or the
DefaultWebSockerServerSession
is already a 1:1 connection between the server and client?
j
I store a list of clients that I am connected to, so if there is an update pushed from somewhere else it dispatches it to those clients that are allowed to receive such information
f
@Joost Klitsie so that means the answer to my question is no? how does it know which client to send the message if I call
send()
? Is it broadcast then?
I'm opening two different connections and they dont receive all the messages
j
well if you have an incoming session it gives you a session
so that only counts for that session
if you want to send things to that session, you'd have to be able to reference it
by for example storing it into a list
and then iterate over all your sessions
and send out the socket events
f
right, but I am already in the session context
I have some objects in a higher scope, that are manipulated in the session scope
I wasnted to confirm this session was between 1:1 client and server
because the examples have this
for (frame in incoming) {
and for me its odd how this gets executed
ah ok I think I got it.
incoming
is a receivechannel and what I am basically doing is configuring a callback for every frame I receive
j
I guess you wanna do something like:
Copy code
webSocket("websocket/path") {
    addActiveSession(this) // whatever you want, just store the session
    incoming.receiveAsFlow().collect { frame ->
        // Handle incoming stuff, or just wait until this is cancelled (aka session ended)
    }
    removeFromActiveSessions(this)
}
then if you want to send a message to all your sessions, check where you stored the sessions and on each you can simply call:
Copy code
sessions.forEach { session ->
    if (session.isActive) {
        connection.outgoing.send(Frame.Binary(true, bytes)) // bytes is in my case a json object converted to byte array
     } else {
         removeFromActiveSessions(session)
     }
}
f
yes, I am already using flow for the output
but the question was the input
that helps a lot