mautrix / imessage

A Matrix-iMessage puppeting bridge
https://go.mau.fi/mautrix-imessage/
GNU Affero General Public License v3.0
356 stars 38 forks source link

Consider Implementing Websocket Failure Recovery Mechanism #186

Closed rollingonchrome closed 9 months ago

rollingonchrome commented 9 months ago

The websocket connection to the BlueBubbles server can fail. Please consider implementing a recovery mechanism.

A websocket failure reports the following error in the sh-imessage bridge log file:

{"level":"error","component":"bluebubbles","error":"websocket: close 1005 (no status)","time":"2024-02-16T10:15:47.873722-08:00","message":"Error reading message from BlueBubbles websocket"}

trek-boldly-go commented 9 months ago

When the websocket to BB fails, it falls into this if statement

        _, payload, err := bb.ws.ReadMessage()
        if err != nil {
            if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
                bb.log.Error().Err(err).Msg("Error reading message from BlueBubbles websocket")
            }
            break
        }

The code that starts the polling should be extracted to a reusable function (out of the Start function) and called during startup and at the end of the PollForWebsocketMessages function.

    ws, _, err := websocket.DefaultDialer.Dial(bb.wsUrl(), nil)
    if err != nil {
        return err
    }
    err = ws.WriteMessage(websocket.TextMessage, []byte("40"))
    if err != nil {
        return err
    }
    bb.ws = ws

    go bb.PollForWebsocketMessages()

It should also have a max retries built in, and progressively sleep longer and longer after each failed attempt to reconnect. Upon successful connection, it should reset the retry count and delay timer.

dltacube commented 9 months ago

You guys work superfast but feel free to assign this to me and I'll take a crack at it. I actually got the code written out for it I think and am testing it now.

dltacube commented 9 months ago

I put something together here: https://github.com/mautrix/imessage/pull/188

Not going to lie, some of that is AI generated but I've done this a million times in other languages and I think it looks good. I'm running my own code locally for now.

trek-boldly-go commented 9 months ago

Closed with PR https://github.com/mautrix/imessage/pull/188