Saturn icon indicating copy to clipboard operation
Saturn copied to clipboard

Is it possible to get a reference to the WebSockets hub outside of a request?

Open markpattison opened this issue 4 years ago • 0 comments

NB - question copied from this SO question, as I would also like to know.

I'm building an application for a toy problem to learn more about SAFE. I have some background processes running server-side and occasionally they need to send a message unprompted to the connected clients. This means that I need a reference to the SocketHub from outside of any particular request.

Currently I have a mutable variable which I pass a value to when the Channel is joined:

let mainChannel = channel {
    join (fun ctx socketId ->
        task {
        printfn "Connected! Main Socket Id: %O" socketId
        let hub = ctx.GetService<Channels.ISocketHub>()

        webSocketHub <- Some hub // Passing the reference to a mutable variable

        task {
            do! Task.Delay 500
            let m = (socketId |> (SetChannelSocketId >> GameData))
            do! (harderSendMessage socketId "message" m "Problem sending SocketId")
            } |> ignore
        return Channels.Ok })
}

However, it seems to me like there should be a better way to get access to the hub - I just can't figure it out.

markpattison avatar May 12 '21 08:05 markpattison