Client does not detect React Native WebSocket close event
See original GitHub issueWhen a websocket connection is broken (for example, when server restarts), what is the recommended way for the client to reestablish the websocket connection to the server?
I’m using apollo server, and I get the following error on the client side, whenever the server breaks the ws connection:
Object {
"error": Event {
"code": 1001,
"isTrusted": false,
"reason": "Stream end encountered",
},
}
In the client websocket link, I can see the error:
class WebSocketLink extends ApolloLink {
private client: Client;
constructor(options: ClientOptions) {
super();
this.client = createClient(options);
}
public request(operation: Operation): Observable<FetchResult> {
return new Observable((sink) => {
return this.client.subscribe<FetchResult>(
{ ...operation, query: print(operation.query) },
{
next: sink.next.bind(sink),
complete: sink.complete.bind(sink),
error: (err) => {
switch (err?.code) {
case 1001:
// error caught here, but how to reconnect?
break;
}
sink.error(err);
},
}
);
});
}
}
Currently, I workaround the problem by re-initializing the Apollo Client instance, but I would like to know if there is an official API to restart the socket connection?
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
react native - web socket on close event is not called in ios
Im using the react native client, any other library uses the same one. im not sure this is a bug in my code...
Read more >WebSocket: close event - Web APIs - MDN Web Docs
Returns a string indicating the reason the server closed the connection. This is specific to the particular server and sub-protocol.
Read more >WebSocket - The Modern JavaScript Tutorial
It responds with “Hello from server, John”, then waits 5 seconds and closes the connection. So you'll see events open → message →...
Read more >How to implement WebSockets in React Native
Creating a WebSockets connection ... The first step is to establish a connection with the server. WebSockets work on their own protocol, ws://...
Read more >How to Avoid Multiple WebSocket Connections in a React ...
To solve this, we can check if the browser tab of the chat app is active. When it's not, we disconnect the user,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve just released a new version which does not care if there is a
wasCleanprop or not. The new,LikeCloseEvent, requires simply the closereasonandcode.Would be absolutely lovely if you can test it out. 😄
Works as advertised!