Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
markpattison opened this issue May 12, 2021 · 0 comments

Comments

@markpattison
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant