sendMessageGrpc function

ThunkAction<AppState> sendMessageGrpc(
  1. String conversationId,
  2. String senderId,
  3. String content
)

Implementation

ThunkAction<AppState> sendMessageGrpc(String conversationId, String senderId, String content) {
  return (Store<AppState> store) async {
    try {
      // Setup gRPC channel and client
      final channel = ClientChannel(
        connectionServer ,
        port: portServer,
        options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
      );
      final client = ChatServiceClient(channel);
      if (kDebugMode) {
        print("Sending message $conversationId");
      }
      // Create gRPC request
      final request = Message_text()
        ..messageId = DateTime.now().millisecondsSinceEpoch.toString()
        ..conversationId = conversationId
        ..senderId = senderId
        ..content = content
        ..timestamp = Int64(DateTime.now().millisecondsSinceEpoch ~/ 1000);

      // Send message via gRPC
      final response = await client.sendMessage_Text(request);

      // Dispatch AddMessageAction with response (if gRPC returns the saved message)
      store.dispatch(AddMessageAction(response));

      await channel.shutdown();
    } catch (e) {
      if (kDebugMode) {
        print("❌ sendMessageGrpc error: $e");
      }
      store.dispatch(SetErrorAction(e.toString()));
    }
  };
}