centrifugal / centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket

Home Page:https://pub.dartlang.org/packages/centrifuge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I am unable to connect to the Centrifugo

Tunmise-Adegoke opened this issue · comments

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<centrifuge.ConnectedEvent>? _connectedSub;
StreamSubscription<centrifuge.ConnectingEvent>? _connectingSub;
StreamSubscription<centrifuge.DisconnectedEvent>? _disconnectedSub;
StreamSubscription<centrifuge.ErrorEvent>? _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,

    ));
// _centrifuge.connect();
// _subscription?.subscribe();

}
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);
});

await _subscription?.subscribe();

}

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;
}
}
}

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.