How do I use the "onunload" event to send a message to the server in SAFE Stack?

317 Views Asked by At

I've got an application built using the SAFE stack where active clients have corresponding objects on the Server side. When a user closes their browser window, I would like a message to be sent to the server to delete that object.

I'm trying to use the "onunload" event like this:


module WindowEvents =
    let unloadSub _ =
        let setUnloadEvent dispatch = 
            Browser.Dom.window.onunload <- (fun _ -> dispatch CloseEvent)
        Cmd.ofSub setUnloadEvent

and I add that to the subscriptions:

Program.mkProgram Model.init Model.update View.render
|> Program.withSubscription WindowEvents.unloadSub

The message is handled in the usual Elmish way and is forwarded on to the Server via the api. However, I find that the message only rarely arrives server-side. Is there something I can do to make this more reliable? Or is there an alternative approach that would be better?

0

There are 0 best solutions below