subscribeToMessageGrpc function
Implementation
ThunkAction<ChatState> subscribeToMessageGrpc(String conversationId){
return (Store<ChatState> store) async{
final channel = ClientChannel(
connectionServer,
port: portServer,
options: const ChannelOptions(
credentials: ChannelCredentials.insecure(),
idleTimeout: null
)
);
final client = ChatServiceClient(channel);
if (kDebugMode) {
print("Starting subcription to messages for $conversationId");
}
try{
final request = GetMessagesRequest()..conversationId = conversationId;
final stream = client.subscribeToMessages(request);
await for (var item in stream){
final msg = Message_text()..messageId = item.messageId
..content = item.content
..senderId = item.senderId
..conversationId = item.conversationId
..timestamp = item.timestamp
..isRead = item.isRead
..editedAt = item.editedAt;
if (kDebugMode) {
print("Received new Message: from ${msg.content}");
}
store.dispatch(AddMessageAction(msg));
}
}catch(e){
if (kDebugMode) {
print("❌ Stream error: $e");
}
// Optionally dispatch error action
} finally {
store.dispatch(SetLoadingAction(false));
await channel.shutdown();
}
};
}