chat support
This commit is contained in:
62
lib/services/chat_realtime_service.dart
Normal file
62
lib/services/chat_realtime_service.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
import 'dart:convert';
|
||||
import 'package:pusher_channels_flutter/pusher_channels_flutter.dart';
|
||||
import '../services/dio_client.dart';
|
||||
|
||||
class ChatRealtimeService {
|
||||
static final PusherChannelsFlutter _pusher =
|
||||
PusherChannelsFlutter.getInstance();
|
||||
|
||||
static Future<void> connect({
|
||||
required int ticketId,
|
||||
required Function(Map<String, dynamic>) onMessage,
|
||||
}) async {
|
||||
|
||||
print(" 🧪🧪🧪🧪🧪🧪🧪🧪🧪SUBSCRIBING TO: private-ticket.$ticketId");
|
||||
|
||||
await _pusher.init(
|
||||
apiKey: "q5fkk5rvcnatvbgadwvl",
|
||||
cluster: "mt1",
|
||||
|
||||
onAuthorizer: (channelName, socketId, options) async {
|
||||
print("🧪🧪🧪🧪🧪🧪🧪🧪🧪🧪AUTHORIZING CHANNEL: $channelName");
|
||||
|
||||
final dio = DioClient.getInstance(options);
|
||||
|
||||
final res = await dio.post(
|
||||
'/broadcasting/auth',
|
||||
data: {
|
||||
'socket_id': socketId,
|
||||
'channel_name': channelName,
|
||||
},
|
||||
);
|
||||
|
||||
return res.data;
|
||||
},
|
||||
|
||||
onEvent: (event) {
|
||||
if (event.eventName == "NewChatMessage") {
|
||||
final data = jsonDecode(event.data);
|
||||
onMessage(Map<String, dynamic>.from(data));
|
||||
}
|
||||
},
|
||||
|
||||
onConnectionStateChange: (current, previous) {
|
||||
print("PUSHER STATE: $current");
|
||||
},
|
||||
|
||||
onError: (message, code, error) {
|
||||
print("PUSHER ERROR: $message");
|
||||
},
|
||||
);
|
||||
|
||||
await _pusher.subscribe(
|
||||
channelName: "private-ticket.$ticketId",
|
||||
);
|
||||
|
||||
await _pusher.connect();
|
||||
}
|
||||
|
||||
static Future<void> disconnect() async {
|
||||
await _pusher.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user