Suave does not provide anything like this, but you can easily extend the example to do this.
The ws socket handler passed to handShake can pass client information externally, and you can create a send / distribute API around it.
The ws function can be changed, for example, as follows
let ws onConnect onDisconnect (webSocket: WebSocket) (context: HttpContext) = let loop () = (* the socket logic stays the same *) socket { onConnect webSocket context try do! loop () finally onDisconnect context }
Then you need to enter onConnect and onDisconnect to register / unregister clients.
I use MailboxProcessor to serialize Connect / Disconnect / Send operations, as an alternative it is easy to use Reactive Extensions or shared mutable parallel storage like ConcurrentDictionary ...
Honza brestan
source share