Managing WebSocket Client Disconnection in a Server-Side Client Pool

Hello, I try to disconnect a client from my "pool of client" when a Websocket closes (server side) :

I've read https://github.com/ethanniser/effect-workshop/blob/0e1d4b7c887fd5c515d332421648271976509ea1/src/part3-webserver/breakpoints/99-effect-client/server/ws.ts this and [SOLVED] Effect HTTP Server Websocket Upgrade

So like in the workshop, I maintain a pool of websocket client through a Ref<Hashmap>. I'd like to remove a client from the map when the connection with that client closes.

In the workshop, it uses import Websocket from "ws" and do raw operations on ws that allows them to create a stream from the first "registration message" that is called StartupMessage in code.

In my case, it's a little bit different since I used the code from the thread linked above and my websocket handler looks like:

gist because message too long ==>
https://gist.github.com/titouancreach/383f3e709acc1930b6ba2039000e2b9a

The problem with this code, is that I only able to identify the client when the client register with the register message, so I need a mechanism to bound a context to the current stream and the context would be the current id of the client.
With imperative code a would have used this.currentClientId = "idididid" when a new client register.
So how I can achieve de removal of client when the connection close (ie when the streams end) ?
Was this page helpful?