Perplexica icon indicating copy to clipboard operation
Perplexica copied to clipboard

Do not kill entire chat window when websocket closes.

Open ProjectMoon opened this issue 1 year ago • 12 comments

Reopens the websocket on close. Prevents badly configured reverse proxies from making the application stop working. In my testing, I've noticed that the websocket simply reopens when the connection is closed. Instead of setting error state to true on websocket close, we just clear the websocket out using the provided React setWs function.

ProjectMoon avatar Jul 30 '24 14:07 ProjectMoon

In case of a valid reason from the backend, if the WS is closed then it would constantly try to reconnect and then the error message would pop up again and again so I cannot merge this.

ItzCrazyKns avatar Jul 30 '24 15:07 ItzCrazyKns

Is there a way to communicate the reason then? There needs to be a better solution than just putting the entire page into error state.

Maybe exponential backoff? Attempt to reconnect immediately, then if it fails, back off for X seconds, then more seconds, and so on.

ProjectMoon avatar Jul 30 '24 15:07 ProjectMoon

Alternatively, can check the error state when onClose. That will make it be able to prevent reconnect attempts if not just a normal close.

I wouldn't just close the entire PR, at least.

ProjectMoon avatar Jul 30 '24 15:07 ProjectMoon

Alternatively, can check the error state when onClose. That will make it be able to prevent reconnect attempts if not just a normal close.

I wouldn't just close the entire PR, at least.

There are certain states when no error is being sent and the WS close

ItzCrazyKns avatar Jul 30 '24 16:07 ItzCrazyKns

In that case, wouldn't we want to try immediate reconnection then? And exponential back off would handle that issue if we cannot reconnect.

ProjectMoon avatar Jul 30 '24 16:07 ProjectMoon

Also, after testing this more locally by killing the backend server and thus the websocket connection, it doesn't appear to attempt to reconnect. But this test only covers a sudden connection drop from the backend.

ProjectMoon avatar Jul 30 '24 17:07 ProjectMoon

Small update: Do not set connection to null if in error state. I'm still trying to work out the best way to do exponential backoff in the current architecture. Do you have any suggestions?

ProjectMoon avatar Jul 30 '24 17:07 ProjectMoon

State does not gets updated instantly (they are asnyc without a callback), if the WS closes and the error state takes time to be set, the page would still be set to null and this would repeat

ItzCrazyKns avatar Jul 31 '24 02:07 ItzCrazyKns

How about something like awaiting the setError, and then using setTimeout with a 0 interval to wait until the next event loop tick for checking the value?

ProjectMoon avatar Jul 31 '24 11:07 ProjectMoon

How about something like awaiting the setError, and then using setTimeout with a 0 interval to wait until the next event loop tick for checking the value?

Wait won't work as it doesn't returns a callback function, the setTimeout method is not efficient, 0 ms interval it would check the value a lot of time and might suck the resources.

ItzCrazyKns avatar Jul 31 '24 14:07 ItzCrazyKns

When using setTimeout with a delay of 0, it forces an execution on next event loop. It's kinda like process.nextTick in node. In any case, what is your suggested way?

ProjectMoon avatar Jul 31 '24 15:07 ProjectMoon

Latest iteration has useRef for reconnecting websocket with exponential backoff. And as a side-note, I learned that useRef existed today.

ProjectMoon avatar Aug 01 '24 15:08 ProjectMoon

@ItzCrazyKns, it makes no sense to close this pull request.

At the moment when the WS connection closes, you have to reload the page because it does not reconnect on its own.

realies avatar Jan 05 '25 15:01 realies

I have the same problem on cloudflared connection - they close web sockets in 100 secunds, thus merging this PR would be very helpful for such scenarios.

s-KaiNet avatar Jan 06 '25 14:01 s-KaiNet

@s-KaiNet, I've reimplemented this again at https://github.com/ItzCrazyKns/Perplexica/pull/555 But the author loves to close PRs instead of providing constructive feedback to the contributors. Let's hope for adequacy. Otherwise, we could just create a better project like this one.

realies avatar Jan 06 '25 15:01 realies

@s-KaiNet, I've reimplemented this again at #555 But the author loves to close PRs instead of providing constructive feedback to the contributors. Let's hope for adequacy. Otherwise, we could just create a better project like this one.

Hey @realies,

I’m not against giving feedback or just closing PRs for no reason. I’ve shared feedback here and on other PRs too. The only ones I close without comments are super low effort or just small fixes like typos.

I kept this PR open so the author could try other ideas or ask for more info. I already left a comment earlier, so I was waiting to see if they needed help.

Just so you know, I work on this project in my free time. Some days I barely get any time at all. But I’m trying to keep Perplexica going and improving it. A lot of similar projects are abandoned, but I’m doing my best to keep this one alive and make it better.

I hope you get where I’m coming from...

Cheers

ItzCrazyKns avatar Jan 07 '25 07:01 ItzCrazyKns

Fixed in #555

ItzCrazyKns avatar Jan 07 '25 07:01 ItzCrazyKns

hey @ItzCrazyKns, but you just replied to the wrong person in your previous comment)) But on the topic... Great project and thank you very much for your efforts here! I can imagine how complicated it might be to manage fast-growing OSS project. This topic is very hot, so you might have even more problems with all the different PRs, issues and user requests, hopefully you will be able to handle it!

s-KaiNet avatar Jan 07 '25 08:01 s-KaiNet