cuuush / groupme-push

GNU General Public License v3.0
2 stars 1 forks source link

Recovering from "Websocket error: Connection to remote host was lost" #4

Open ryanjflynn opened 6 months ago

ryanjflynn commented 6 months ago

I'm trying to use this package for coding a bot app, but I'm consistently finding that after some time (varies, usually less than a couple hours) I'm seeing the logging produce ERROR:groupme-push:Websocket error: Connection to remote host was lost. ERROR:websocket:Connection to remote host was lost. - goodbye

and then the script terminates. I'm not sure if there is a way to pin down what is causing the connection to be lost, but more importantly I'm wondering what can be down to recover form this and restart the connection. In my script calling client.start() it seems like are not any exceptions being thrown/caught when it happens, so I'm not sure how to detect that the PushClient has stopped running once it gets the connection error.

SZRabinowitz commented 6 months ago

Hello. In my fork biden2020prez/groupme-push, it reconnects whenever that error happens. Install it using pip install git+https://github.com/biden2020prez/groupme-push --force.

Also, there is a good chance that those fixes will be added to this repository in the future.

SZRabinowitz commented 6 months ago

to give more details, you should add an argument called reconnect when initializing the pushclient, with an integer of how many seconds it should try to reconnect after an error.

for example, this is how I do it in my userbot

client = PushClient(access_token=api_key, on_message=handle_messages.on_message, disregard_self=ignore_self, reconnect=20)

In that example, if it has an error, it will reconnect after 20 seconds. It would probably also work if you used 1 second, I don't know why I didn't.

cuuush commented 6 months ago

Will work on a fix for this, thanks Biden for suggesting your fork :)

SZRabinowitz commented 6 months ago

Hi @cuuush. I simply pass the reconnect option that the user specifies over to websocket-client, which has a reconnect option. Should be simple :)

SZRabinowitz commented 6 months ago

Once you are at it...

I added two lines here which fixed errors when internet gets disconnected:

           else:
                print(
                    "Groupme got unhandled message: {}".format(
                        json.dumps(message, indent=4)
                    )
                )
                self.ws.close()
                self.start()

It was a patch, more than a fix. Basically groupme was sending an error about needing to reconnect. Unfortunately, I dont have the log anymore to tell you exactly what the error was

cuuush commented 6 months ago

Ha, that’s smart. I think GroupMe’s faye web socket sever has a max alive time, in my experience connections over 24h get killed. So we can just reconnect if it fails. Maybe we should also only try reconnecting a couple times in case there’s some other issue. Like the server is down or broken, so we don’t flood it.

SZRabinowitz commented 6 months ago

Hmm... I guess that's an option. Or if it fails to reconnect more than once, it can wait one hour before trying again

SZRabinowitz commented 6 months ago

I recall having another issue that the server closes the connection without an error, so reconnect and on_error doesn't work. But I have a bot running for a week now without issues, so I don't remember much about it.

SZRabinowitz commented 6 months ago

I am now looking at the changes I made in my fork.

image

I did that so users can catch errors starting the bot

ryanjflynn commented 6 months ago

Thank you guys for taking a look and thanks @biden2020prez for the info and example from your fork!