How to implement server-push over websocket in suave?
Asked Answered
B

2

9

can I write something like this

let echo (ws: WebSocket) =
    fun ctx -> socket {
        let loop = ref true            
        while !loop do
            let! message = Async.Choose (ws.read()) (inbox.Receive())
            match message with
            | Choice1Of2 (wsMessage) ->
                match wsMessage with
                | Ping, _, _ -> do! ws.send Pong [||] true
                | _ -> ()
            | Choice2Of2 pushMessage -> do! ws.send Text pushMessage true
    }

or do I need 2 seperate socket-loop for concurrent read-write?

Brasilein answered 9/10, 2015 at 5:49 Comment(0)
N
9

I think you could solve this using Async.Choose (there is a bunch of implementations - though I'm not sure where is the most canonical one).

That said, you can certainly create two loops - the reading one inside socket { .. } so that you can receive data from web sockets; the writing one can be ordinary async { ... } block.

Something like this should do the trick:

let echo (ws: WebSocket) =  
    // Loop that waits for the agent and writes to web socket
    let notifyLoop = async { 
      while true do 
        let! msg = inbox.Receive()
        do! ws.send Text msg }

    // Start this using cancellation token, so that you can stop it later
    let cts = new CancellationTokenSource()
    Async.Start(notifyLoop, cts.Token)

    // The loop that reads data from the web socket
    fun ctx -> socket {
        let loop = ref true            
        while !loop do
            let! message = ws.read()
            match message with
            | Ping, _, _ -> do! ws.send Pong [||] true
            | _ -> () }
Negrito answered 9/10, 2015 at 14:10 Comment(2)
could you please suggest a good Async.Choose-implementation for this case? and about too loop: is this good? thank!Brasilein
I think your implementation of two-loop have thread-safe problem (write from 2 threads)Brasilein
B
2

There isn't a proper implementation of Async.Choose (for this case at least), so we need two async-loop for concurrent read-write; see this for more detail

Brasilein answered 9/10, 2015 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.