Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

"Client is not connected to a websocket" #55

Closed
booboothefool opened this issue Dec 23, 2016 · 9 comments
Closed

"Client is not connected to a websocket" #55

booboothefool opened this issue Dec 23, 2016 · 9 comments

Comments

@booboothefool
Copy link

booboothefool commented Dec 23, 2016

Sorry for the ambiguity, but sometimes I get this error when I start my app.

I am pretty sure I set up most things correctly because I have subscriptions flying all over my app, and it does connect about 90% of the time, but it's a real bummer when it doesn't (nothing realtime works of course) and I have no idea why. :c

screen shot 2016-12-23 at 2 14 00 am

@helfer
Copy link
Contributor

helfer commented Jan 10, 2017

@booboothefool I think this might happen if you subscribe when the websocket isn't ready yet. Could you try to submit a failing test case for this? It should be feasible to do so by creating a test case which subscribes as quickly as possible after initializing the client.

@Urigo
Copy link
Contributor

Urigo commented Feb 5, 2017

Since #53 merged, there is a new INIT message in the protocol, and new subscriptions are initialized only after init is done.
@booboothefool , Can you please try with the new version? we will release it really soon.

@NeoPhi
Copy link
Contributor

NeoPhi commented Feb 10, 2017

@Urigo I'm not seeing in the code how the INIT message acts as a guard to prevent subscription messages from being sent. Additionally in the case of a reconnect previous subscriptions are reestablished prior to the INIT event firing.

@booboothefool
Copy link
Author

booboothefool commented Feb 11, 2017

@Urigo

I no longer get

Client is connected to a websocket

and it seems to always connect successfully initially, but now instead I am getting an error that reads

INVALID_STATE_ERR

It seems to be a lot worse because it happens much more frequently, and while Client is not connected to a websocket would just error and break subscriptions, INVALID_STATE_ERR crashes my app completely.

I am not sure why this is happening, as it seems completely random. It's like it's dropping the connection. Well when it happens every time I restart my server, so the reconnect might be what's messing it up, but it also happens in production too where my server isn't restarting.

All I did was I upgraded to the new library and changed addGraphQLSubscriptions to the one from the library e.g. import { addGraphQLSubscriptions } from 'subscriptions-transport-ws'; instead of https://github.com/apollographql/GitHunt-React/blob/master/ui/helpers/subscriptions.js. Is there anything important that I missed?

My subscriptions look something like this (based on http://dev.apollodata.com/react/receiving-updates.html#Subscriptions):

  componentWillReceiveProps(newProps) {
    const { user } = this.props;
    if (!newProps.data.loading) {
        if (this.subscription) {
          if (
            // if the feed has changed, we need to unsubscribe before resubscribing
            newProps.data.favoritedYou !== this.props.data.favoritedYou ||
            newProps.data.visitedYou !== this.props.data.visitedYou
          ) {
            this.subscription.unsubscribe();
          } else {
            // we already have an active subscription with the right params
            return;
          }
        }
        this.subscription = [
          newProps.data.subscribeToMore(newFavorites(user)),
          newProps.data.subscribeToMore(newVisitors(user)),
        ];
    }
  }

@NeoPhi
Copy link
Contributor

NeoPhi commented Feb 11, 2017

@booboothefool I've noticed that with native WebSockets I'm seeing the client disconnect and reconnect more often. On the server do you have the keepAlive option set? If not does setting it to something like 20000 help reduce the incidence?

@booboothefool
Copy link
Author

booboothefool commented Feb 14, 2017

@NeoPhi cool so:

0.3.1 => "Client is not connected to a websocket" (no subscriptions work)
0.5.0 => "INVALID_STATE_ERR" (crashes app)

Then I upgraded to 0.5.1 and did what you suggested setting keepAlive: 20000 on the server, and it seems like it works great now. Client always connects successfully and it hasn't crashed me with INVALID_STATE_ERR in 2 days. Overall happy with it because this is the best I've seen my subscriptions work. 👍

Can you explain what that does and how it solves the problem (and what the problem actually is)?

@NeoPhi
Copy link
Contributor

NeoPhi commented Feb 14, 2017

Without keepAlive unless you have traffic constantly flowing across the WebSocket you are probably hitting a connection timeout with a proxy or firewall.

I've not walked through the code but I suspect that there is a race condition between the WebSocket reconnecting and the previously active subscriptions being reestablished such that the client thinks it is okay to send a message when in fact it shouldn't, resulting in the error

@sairamdevarashetty
Copy link

sairamdevarashetty commented Apr 24, 2017

+1
@Urigo

facing the same error again using version 0.5.5

@vladinator1000
Copy link

vladinator1000 commented May 23, 2017

To more easily debug issues like this, please look at the Network -> Frames tab in Chrome devtools. That's where any errors would be.
image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants