How to make sure a CustomMessageHandler (js in the UI) is registered before sending messages (server function)

I'm using the firebase package to handle login to my app.
Unfortunately, half of the time, the server function sends a custom message before the js script to register the CustomMessageHandler is ran.
Is there a way to make sure the UI is "ready", i.e. all/or that piece of js, before letting the server function send that message?
Or to let shiny know the handler will come later and keep the message?