-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Error: Connection closed" even when connection is open #1565
Comments
➤ Automation for Jira commented: The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-4019 |
It seems like this error might be only caused in React dev mode since it calls hooks twice. Still curious about why we can't catch the error 🤔 |
Hey again @TranquilMarmot, Apologies for the confusion here - the reason try catch isn't working is because For now, until we've fixed that issue, I think you probably would have to manually debug where the error is being thrown from I'm afraid. From the looks of it, it would seem to me that in react strict mode your |
Does that mean that the error is happening inside of a promise in It would be nice if |
I think we may have gotten around this by tracking when we've already closed the connection in the hook: export const useAblyRealtimeClient = (authParams?: Record<string, string>) => {
const clientRef = useRef<Realtime | null>(null);
const hasClosed = useRef(false);
useEffect(() => {
// don't create the ably client on the server because it will cause errors
if (typeof window !== "undefined") {
clientRef.current = createAblyRealtimeClient(authParams);
hasClosed.current = false;
}
return () => {
if (
!hasClosed.current &&
clientRef.current &&
["initialized", "connected", "connecting"].includes(
clientRef.current?.connection.state,
)
) {
clientRef.current?.close();
hasClosed.current = true;
}
clientRef.current = null;
};
}, [authParams?.chatId]);
return clientRef.current;
}; |
I have the same issue only in dev mode, no such error on stage |
@VeskeR @owenpearson I also noticed that this error occurs after unmout in
Example of how
I am using the latest version of ably - |
In our Next.js app, we're creating / closing an Ably realtime client inside of a
useEffect
.This client is then passed as a prop to an
AblyProvider
.However, this still causes the Ably client to throw an error. Next.js will show us this:
![image](https://private-user-images.githubusercontent.com/504259/295022798-9e1e4921-f382-433e-a6d2-a72bc218064e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIyNjg2NzcsIm5iZiI6MTcyMjI2ODM3NywicGF0aCI6Ii81MDQyNTkvMjk1MDIyNzk4LTllMWU0OTIxLWYzODItNDMzZS1hNmQyLWE3MmJjMjE4MDY0ZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzI5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyOVQxNTUyNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zNDg3NWI3ODJiMjMwZjE4OWM0ZGY2YzgxNGQ3OGQwMzZkOTc1NTllYzc4YmUzZDcyZjg3ZjI4NzdhMzBjMzQ1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.FsZXPpDsfHev_9--IRIg4nusCGFhk-9Y1mj1AMKZ1GM)
More of a stack trace...
Some questions...
What status are we supposed to check for when closing the connection? If I log
clientRef.current?.connection.state
before we callclientRef.current?.close();
it's shown asconnecting
orconnected
.Why can't we
catch
this error? Even with theclose()
call inside of atry
/catch
, it isn't being caught (maybe this is an issue inside Next.js?)┆Issue is synchronized with this Jira Bug by Unito
The text was updated successfully, but these errors were encountered: