Skip to content
FSharp.Control.WebSockets wraps dotnet WebSockets in FSharp friendly functions and has a ThreadSafe version.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github init Sep 19, 2018
docs/coverage Bump version to 0.2.0 May 24, 2019
.gitattributes init Sep 19, 2018
.gitignore ignore May 24, 2019
fsc.props init Sep 19, 2018
paket.dependencies Fix deps, add tpl tests proj May 24, 2019


FSharp.Control.WebSockets wraps dotnet websockets in FSharp friendly functions and has a ThreadSafe version.


Dotnet websockets only allow for one receive and one send at a time. If multiple threads try to write to a websocket, it will throw a System.InvalidOperationException with the message There is already one outstanding 'SendAsync' call for this WebSocket instance. ReceiveAsync and SendAsync can be called simultaneously, but at most one outstanding operation for each of them is allowed at the same time.. This wraps a websocket in a FIFO that allows for multiple threads to write or read at the same time. See


MacOS/Linux Windows
Travis Badge Build status
Build History Build History


Name Stable Prerelease
FSharp.Control.Websockets NuGet Badge NuGet Badge
FSharp.Control.Websockets.TPL NuGet Badge NuGet Badge


open System
open System.Net
open System.Net.WebSockets
open System.Threading.Tasks
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open FSharp.Control.Websockets

let echoWebSocket (httpContext : HttpContext) (next : unit -> Async<unit>) = async {

    if httpContext.WebSockets.IsWebSocketRequest then
        let! websocket  = httpContext.WebSockets.AcceptWebSocketAsync() |> Async.AwaitTask
        // Create a thread-safe WebSocket from an existing websocket
        let threadSafeWebSocket = ThreadSafeWebSocket.createFromWebSocket websocket
        while threadSafeWebSocket.State = WebSocketState.Open do
                let! result =
                    |> ThreadSafeWebSocket.receiveMessageAsUTF8
                match result with
                | Ok(WebSocket.ReceiveUTF8Result.String text) ->
                    //Echo it back to the client
                    do! WebSocket.sendMessageAsUTF8 text websocket
                | Ok(WebSocket.ReceiveUTF8Result.StreamClosed (status, reason)) ->
                    printfn "Socket closed %A - %s" status reason
                | Error (ex) ->
                    printfn "Receiving threw an exception %A" ex.SourceException
            with e ->
                printfn "%A" e

        do! next()


//Convenience function for making middleware with F# asyncs and funcs
let fuse (middlware : HttpContext -> (unit -> Async<unit>) -> Async<unit>) (app:IApplicationBuilder) =
    app.Use(fun env next ->
                middlware env (next.Invoke >> Async.AwaitTask)
                |> Async.StartAsTask :> Task)

let configureEchoServer  (appBuilder : IApplicationBuilder) =
    |> Server.fuse (echoWebSocket)
    |> ignore

let getKestrelServer configureServer uri = async {
    let configBuilder = new ConfigurationBuilder()
    let configBuilder = configBuilder.AddInMemoryCollection()
    let config = configBuilder.Build()
    config.["server.urls"] <- uri
    let host = WebHostBuilder()
                .Configure(fun app -> configureServer app )

    do! host.StartAsync() |> Async.AwaitTask
    return host


Make sure the following requirements are installed in your system:

> build.cmd // on windows
$ ./  // on unix

Environment Variables

  • CONFIGURATION will set the configuration of the dotnet commands. If not set it will default to Release.
    • CONFIGURATION=Debug ./ will result in things like dotnet build -c Debug
  • GITHUB_TOKEN will be used to upload release notes and nuget packages to github.
    • Be sure to set this before releasing

Watch Tests

The WatchTests target will use dotnet-watch to watch for changes in your lib or tests and re-run your tests on all TargetFrameworks

./ WatchTests


git add .
git commit -m "Scaffold"
git remote add origin origin
git push -u origin master
paket config add-token "" 4003d786-cc37-4004-bfdf-c4f3e8ef9b3a
  • Create a GitHub OAuth Token

    • You can then set the GITHUB_TOKEN to upload release notes and artifacts to github
    • Otherwise it will fallback to username/password
  • Then update the with a new version, date, and release notes ReleaseNotesHelper

#### 0.2.0 - 2017-04-20
* FEATURE: Does cool stuff!
* BUGFIX: Fixes that silly oversight
  • You can then use the Release target. This will:
    • make a commit bumping the version: Bump version to 0.2.0 and add the release notes to the commit
    • publish the package to nuget
    • push a git tag
./ Release

Code formatting

To format code run the following target

./ FormatCode

This uses Fantomas to do code formatting. Please report code formatting bugs to that repository.

You can’t perform that action at this time.