graphql-flutter icon indicating copy to clipboard operation
graphql-flutter copied to clipboard

How to know that web socket is connected in subscription widget.

Open anirudhsharma392 opened this issue 4 years ago • 7 comments

I am using a subscription widget and how can I make sure that connection is established with the server and I can call it in the subscription widget.

here is my socket client connection

 link = HttpLink(
      uri:
          'http://staginggraphql*********************,
      headers: <String, String>{
        'x-token': centralstate.GraphQLToken,
      },
    );

    socketLink = WebSocketLink(
      url: 'ws://staginggraphql*********************,
      config: SocketClientConfig(
        initPayload: {"authToken": "${centralstate.GraphQLToken}"},
        autoReconnect: true,
        inactivityTimeout: Duration(seconds: 30),
      ),
    );

    link = link.concat(socketLink);

    client = ValueNotifier(
      GraphQLClient(cache: InMemoryCache(), link: link),
    );

Subscription widget:

GraphQLProvider(
          client: client,
          child: Subscription(
            operationName,
            """subscription $operationName{
    leagueGameStatusUpdate(leagueAppID:"${centralstate.leagueAppID}"){
          appID
          error
          status
            }
            }""",
            builder: ({
              bool loading,
              dynamic payload,
              dynamic error,
            }) {
             


//how to make sure that WebSocket is connected and then call playful function


              game.playDuel().then((value) {
                if (value != 'complete') {
                  print('error screen');
                }
              });

              return PlayingAnimation();
            },
          )),

loading and payload always returns true and null I need to give some delay before calling game.playDuel() otherwise it will not work. I know I can ask it on stack overflow but I am not getting any response over there. Sorry but I need to fix this asap. thanks, any help would be appreciated.

anirudhsharma392 avatar Feb 27 '20 07:02 anirudhsharma392

+1. I would also like to know this event.

eliezedeck avatar Mar 07 '20 15:03 eliezedeck

I have a PR for this here: https://github.com/zino-app/graphql-flutter/pull/580

eliezedeck avatar Mar 10 '20 04:03 eliezedeck

Hey @eliezedeck , did you figure out how to know this event??

anirudhsharma392 avatar Mar 19 '20 05:03 anirudhsharma392

Hey @eliezedeck , did you figure out how to know this event??

Yes, see the PR above.

eliezedeck avatar Mar 19 '20 08:03 eliezedeck

if I incorporate the changes. It's giving me error that subscribe was called on null.

anirudhsharma392 avatar Mar 19 '20 08:03 anirudhsharma392

I suspect there is something wrong with how you're using the library. That should probably be something easy to fix though. I'm sorry but I don't have the time resource to look into this for the time being.

EDIT: I'm using this myself, and I'm not having the same problem as you do. So, that's a fact to consider.

eliezedeck avatar Mar 19 '20 12:03 eliezedeck

Try 4.0.0-alpha.6, which has a revitalized version of WebSocketLink and an overhauled subscription api. It also adds a Might make what you're trying to do easier.

It doesn't really expose the connection state. Maybe we should add loading responses to the stream while reconnection attempts are being made.

micimize avatar Sep 14 '20 16:09 micimize