knaeckekami / gql_phoenix_link Goto Github PK
View Code? Open in Web Editor NEWA gql Link for communicating over Phoenix Channels
License: MIT License
A gql Link for communicating over Phoenix Channels
License: MIT License
Hello
when i use PhoenixLink with SubscriptionChannel class, i try to close subscription but i see after the closing, some logs with
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();
}
}
}
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.