Web sites in Suave

I studied using websites with a Suave web server. Unfortunately, this is not well documented, and all I managed to find was the following: https://github.com/SuaveIO/suave/tree/master/examples/WebSocket

However, this only shows that the webcam is responding to the client who made the request, and I want the socket in principle to respond to all connected clients. Something like a chat server.

I have used SignalR in the past, but would prefer to avoid this.

So, how can I get the Suave server to send data to all connected websocket clients?

+7
f # websocket suave
source share
1 answer

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 ...

+7
source share

All Articles