Closed Tunmise-Adegoke closed 1 year ago
Hello, @Tunmise-Adegoke
Sorry, It's pretty unreadable description. There could be many reasons of why you can't connect to Centrifugo. If you think it's a bug then please open a proper bug report – with steps to reproduce, nicely formatted code, client/server versions and maybe using our console example as the base: https://github.com/centrifugal/centrifuge-dart/tree/master/example/console.
Otherwise, I can't guess what was the issue. Can be literally anything.
import 'dart:async'; import 'dart:convert'; import 'package:centrifuge/centrifuge.dart' as centrifuge; import 'package:flutter/material.dart'; import 'package:mize/constants/api_endpoints.dart'; import 'package:mize/models/notification_model.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'dart:developer' as dev;
class NotificationClient { late centrifuge.Client _centrifuge; centrifuge.Subscription? _subscription; StreamSubscription? _connectedSub;
StreamSubscription? _connectingSub;
StreamSubscription? _disconnectedSub;
StreamSubscription? _errorSub;
final services = StreamController();
Stream get notifications => services.stream;
// void init() { // debugPrint('connected'); // const url = ApiEndpoints.websocketurl; // _centrifuge = centrifuge.createClient( // url, // centrifuge.ClientConfig( // token: ApiEndpoints.centrifugoToken, // )); // _centrifuge.connect(); // _subscription?.subscribe(); // }
NotificationClient() { // debugPrint('connected'); const url = ApiEndpoints.websocketurl; _centrifuge = centrifuge.createClient( url, centrifuge.ClientConfig( token: ApiEndpoints.centrifugoToken,
} Future connect() async {
_centrifuge.connected.listen((event) {
// onConnect();
Fluttertoast.showToast(
msg: "Connected to Centrifugo server",
backgroundColor: Colors.green,
textColor: Colors.white);
});
_centrifuge.connecting.listen((event) {
// onConnect();
Fluttertoast.showToast(
msg: "Connecting to Centrifugo server",
backgroundColor: Colors.green,
textColor: Colors.white);
});
_centrifuge.disconnected.listen((event) {
Fluttertoast.showToast(
msg: "Centrifugo server disconnected",
backgroundColor: Colors.red,
textColor: Colors.white);
});
_centrifuge.error.listen((event) {
Fluttertoast.showToast(
msg: 'error', backgroundColor: Colors.red, textColor: Colors.white);
print(event);
});
await _centrifuge.connect();
}
Future subscribe(String channel) async {
_subscription = _centrifuge.getSubscription(channel)!;
_subscription?.publication.listen((event) {
final data = utf8.decode(event.data);
final notification = json.decode(data);
final body = notification['body'].toString();
final message = notification['message'].toString();
services.sink.add(NotificationModel(body: body, message: message));
});
_subscription?.subscribing.listen((event) {
Fluttertoast.showToast(
msg: "Subscribing to Centrifugo server",
backgroundColor: Colors.green,
textColor: Colors.white);
});
_subscription?.subscribed.listen((event) {
Fluttertoast.showToast(
msg: "Subscribed to Centrifugo server",
backgroundColor: Colors.green,
textColor: Colors.white);
});
_subscription?.unsubscribed.listen((event) {
Fluttertoast.showToast(
msg: "unsubscribing to Centrifugo server",
backgroundColor: Colors.green,
textColor: Colors.white);
});
}
Future dispose() async {
await _connectedSub?.cancel();
await _connectingSub?.cancel();
await _disconnectedSub?.cancel();
await _errorSub?.cancel();
}
void listenToNotifications(NotificationModel noti) async { final output = jsonEncode({'body': noti.body, 'message': noti.message}); print('Sending msg : $output'); final data = utf8.encode(output); try { await _subscription?.publish(data); } on Exception { rethrow; } } }