Realtime Messages With WebSockets
It’s easy to set up WebSockets with Suave.
First, define a function that takes
HttpContext typed parameters, and returns a socket computation expression:
Next, use the
send function to receive and send messages to the clients:
Then, use the
handShake function to fit it in your web server:
The complete example can be found here.
Handling connection errors
By default the socket computation expression handles any errors transparently on both writing and reading from the websocket shutting down the connection.
You may want to add your own additional error handling logic to catch and handle any errors raised when reading and writing from a websocket. Some scenarios where this may be useful:
- Alerting the rest of the application that a connection is closed.
- Unsubscribing and/or shutting down processes used by the websocket connection.
- Custom logging of the error.
Example code can be found here.