Skip to content
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

Able to detect network lost? #53

Closed
yoonwaiyan opened this issue Jul 16, 2019 · 1 comment
Closed

Able to detect network lost? #53

yoonwaiyan opened this issue Jul 16, 2019 · 1 comment
Labels

Comments

@yoonwaiyan
Copy link

yoonwaiyan commented Jul 16, 2019

Hi there, sorry if my issue is silly, I'm still amateur in websockets handling. I'm facing a problem where I couldn't disconnect properly using sockette when the network is offline. I'm currently using sockette to connect with my Websocket API using AWS API Gateway, everything seems fine except when I realized that the connections created by the gateway isn't always being disconnected thus leaving many dead connection records in my DynamoDB. This is how I replicate the issue:

  1. Open new tab and access to my website with websocket connection, wait until it's connected
  2. Turn off my laptop WiFi connection, to simulate offline connection
  3. Close the tab
  4. Turn on my laptop WiFi connection

I'm wondering do Sockette handles such situation, at least have a callback when offline connection is detected? If it's already available, how can I modify my code to cater for that? This is my code in React:

useEffect(
  () => {
    if (isProduction) {
      webSocket = new Sockette(webSocketUrlFor(userId), {
        timeout: 5e3,
        onopen: e => console.log('Connected', e),
        onmessage: onReceivedMessage,
        onreconnect: () => console.log('Reconnecting...'),
        onmaximum: e => console.log('Stop Attempting!', e),
        onclose: e => {
          console.log('Closed connection. Reconnecting...', e);
          webSocket.reconnect();
        },
        onerror: e => console.log('Error:', e)
      });

      return () => {
        webSocket.close();
      };
    }
  },
  [userId]
);
@lukeed
Copy link
Owner

lukeed commented Jul 16, 2019

Hey, not silly!

In this case you would have to catch/listen for the disconnection from the server side's point of view. This is because your client unexpectedly shuts down (for whatever reason), so there's no chance to send a ws.close() or even a Going Away code. The network is gone so there's no ability to send anything.

I don't use AWS for WS, so I can't help much beyond linking to what looks to be relevant docs.

Hope that helps!

@lukeed lukeed closed this as completed Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants