Client does not detect React Native WebSocket close event

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
enisdenjocommented, Dec 3, 2020

I’ve just released a new version which does not care if there is a wasClean prop or not. The new, LikeCloseEvent, requires simply the close reason and code.

Would be absolutely lovely if you can test it out. 😄

1reaction
kendrickwcommented, Dec 3, 2020

Works as advertised!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found