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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fresh_graphql] Link for Subscription #50

Open
Fintasys opened this issue Jun 27, 2021 · 3 comments
Open

[fresh_graphql] Link for Subscription #50

Fintasys opened this issue Jun 27, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Fintasys
Copy link

Fintasys commented Jun 27, 2021

Great idea this package and exactly what I was looking for. 馃檱
Based on this idea it would be possible to create a similar link implementation for subscription.

What do you think?

This is my current code that could be replaced

class WSLink extends Link {
  /// Creates a new [WebSocketLink] instance with the specified config.
  WSLink(
    this.url, {
    this.headers = const {},
    this.getToken,
  });

  final String url;
  final Map<String, dynamic> headers;
  final Future<String?>? getToken;

  // cannot be final because we're changing the instance upon a header change.
  SocketClient? _socketClient;

  @override
  Stream<Response> request(Request request, [forward]) async* {
    // Get Token or Refresh
    if (getToken != null) {
      var token = await getToken;
      headers['Authorization'] = 'Bearer $token';
    }

    if (_socketClient == null) {
      connectOrReconnect();
    }

    yield* _socketClient!.subscribe(request, true);
  }

  /// Connects or reconnects to the server with the specified headers.
  void connectOrReconnect() {
    _socketClient?.dispose();
    _socketClient = SocketClient(
      url,
      config: SocketClientConfig(
        inactivityTimeout: Duration(seconds: 30),
        delayBetweenReconnectionAttempts: const Duration(seconds: 2),
        autoReconnect: true,
        connect: (url, protocols) => IOWebSocketChannel.connect(
          url,
          protocols: protocols,
          headers: headers,
        ),
      ),
    );
  }

  /// Disposes the underlying socket client explicitly. Only use this, if you want to disconnect from
  /// the current server in favour of another one. If that's the case, create a new [WebSocketLink] instance.
  Future<void> dispose() async {
    await _socketClient?.dispose();
    _socketClient = null;
  }
}
@Fintasys Fintasys changed the title Link for Subscription [fresh_graphql] Link for Subscription Jun 27, 2021
@fikretsengul
Copy link

Yes, It would be great. I need this also. Thank you.

@cacianokroth
Copy link

馃憖

@felangel felangel added the enhancement New feature or request label Sep 10, 2021
@felangel felangel self-assigned this Sep 10, 2021
@Centauride
Copy link

Centauride commented Dec 13, 2022

It would be nice to have this feature implemented, i'm really struggling to make it work :'(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants