Coder Social home page Coder Social logo

gql_phoenix_link's Introduction

Hi there πŸ‘‹

gql_phoenix_link's People

Contributors

knaeckekami avatar reddwarf03 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gql_phoenix_link's Issues

Heartbeat

Hello

when i use PhoenixLink with SubscriptionChannel class, i try to close subscription but i see after the closing, some logs with
Capture d’écran 2022-08-09 aΜ€ 12 08 43
Do you know how to close completely the connection please

Thx

// Dart imports:
import 'dart:async';

// Package imports:
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:phoenix_socket/phoenix_socket.dart';

// Project imports:
import 'package:core/util/confirmations/phoenix_link.dart';

class SubscriptionChannel {
  PhoenixSocket? socket;
  PhoenixChannel? channel;
  GraphQLClient? client;

  final StreamController<Map> _onMessageController = StreamController<Map>();
  Stream<Map> get onMessage => _onMessageController.stream;

  Future<void> connect(
      String phoenixHttpLinkEndpoint, String websocketUriEndpoint) async {
    final HttpLink phoenixHttpLink = HttpLink(
      phoenixHttpLinkEndpoint,
    );

    channel =
        await PhoenixLink.createChannel(websocketUri: websocketUriEndpoint);
    final phoenixLink = PhoenixLink(
      channel: channel!,
    );

    var link = Link.split(
        (request) => request.isSubscription, phoenixLink, phoenixHttpLink);
    client = GraphQLClient(
      link: link,
      cache: GraphQLCache(),
    );
  }

  void addSubscriptionTransactionConfirmed(
      String address, Function(QueryResult) function) {
    final subscriptionDocument = gql(
      'subscription { transactionConfirmed(address: "$address") { nbConfirmations } }',
    );

    Stream<QueryResult> subscription = client!.subscribe(
      SubscriptionOptions(document: subscriptionDocument),
    );
    subscription.listen(function);
  }

  Future<Message> onPushReply(Push push) async {
    final Completer<Message> completer = Completer<Message>();
    final Message result = await channel!.onPushReply(push.replyEvent);
    completer.complete(result);
    return completer.future;
  }

  void close() {
    _onMessageController.close();
    if (socket != null) {
      socket!.close();
    }
    if (channel != null) {
      channel!.close();
    }
  }
}

Example usage

For people looking for full working example
call initGraphQLChannel() from initstate

void initGraphQLChannel() async {
    // your absinthe plug api location
    final HttpLink phoenixHttpLink = HttpLink(
      'http://192.168.1.23:4000/api',
    );

    // your websocket location
    String websocketUri = "ws://192.168.1.23:4000/socket/websocket";

    // creation of phoenixChannel for use in PhoenixLink
    final phoenixChannel =
        await PhoenixLink.createChannel(websocketUri: websocketUri);

    // creation of PhoenixLink
    final phoenixLink = PhoenixLink(
      channel: phoenixChannel,
    );

    // Split like in flutter graphql documentation between websocket and http requests
    var _link = Link.split(
        (request) => request.isSubscription, phoenixLink, phoenixHttpLink);
    final GraphQLClient client = GraphQLClient(
      link: _link,
      cache: GraphQLCache(),
    );

    // your subscription query
    final subscriptionDocument = gql(
      r'''
    subscription{
  postAdded{
    id
    name
    description
  }
}
  ''',
    );

    // subscribe and listen
    Stream<QueryResult> subscription = client.subscribe(
      SubscriptionOptions(document: subscriptionDocument),
    );
    subscription.listen(reactToPostCreated);
  }

  void reactToPostCreated(QueryResult event) {
    print("Received event =>");
    print(event);
  }

  // example event received structure
  // Received event =>
  // I/flutter (15033): QueryResult(source: QueryResultSource.network, data: {postAdded: {__typename: Post, id: 32, name: graphqllink post, description: something something}}, context: Context({ResponseExtensions: Instance of 'ResponseExtensions'}), exception: null, timestamp: 2021-09-02 20:15:40.365330)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.