Implementing gRPC in iOS React Native App
As described in this issue , we cannot implement a gRPC client using a Node implementation because "RN is not a pure Node".
So, I started working on my Objective-C implementation using Native Modules .
[service postWithRequest:request handler:^(RequestConfirmation * _Nullable response, NSError * _Nullable error) {
if (response) {
// This prints correctly in my JS console
RCTLogInfo(@"%@", response.message);
// This generates an error, see below
resolve(response);
// This works
NSDictionary *formattedResponse = @{
@"id": response.id_p,
@"message": response.message
};
resolve(formattedResponse);
} else {
reject(@"error", @"An error occurred while saving", error);
}
}];
Mistake:
RCTJSONStringify() encountered the following error: Invalid type in JSON write (RequestConfirmation)
As you can see, the problem is with the method resolve
. I suppose React doesn't find a way to convert my proto message to JSON.
How can I save the answer as it is and pass it to the resolution method? Then I can decrypt it in my JS code.
Thank.
EDIT 1:
RequestConfirmation
is defined in my file proto
like this:
message RequestConfirmation {
string id = 1;
string message = 2;
}
And then it is generated in Objective-C:
@interface RequestConfirmation : GPBMessage
@property(nonatomic, readwrite, copy, null_resettable) NSString *id_p;
@property(nonatomic, readwrite, copy, null_resettable) NSString *message;
@end
source to share
Perhaps there is a potential solution for this.
Incredible Engineering Coded:
- grpc-web - Typescript / Javascript, browser or NodeJS!
In addition to the unfortunate naming, it means that it is not a simple fork official, C ++ - using, grpc - web project ).
Defining the unclean project project grpc-web-client page :
This library is designed to use both JavaScript and TypeScript from a browser or Node.js
The possible advantages of the Improbable version are as follows: 1. It does not appear to use native code (i.e. No C / C ++ / Java) 2. It can generate TypeScript (therefore JavaScript) for Node.JS
So, perhaps we could work GRPC-on-RN when point # 2 is associated with a NodeJS-on-RN project like rn-nodeify .
Please provide feedback if you have any progress with this.
source to share