gRPC on React Native

Viewed 6427

I've been working with iOS for a while now, and I'm experiementing with React Native. An obstacle I've come accross is how I can incorporate my gRPC endpoints into React Native, or if that fundamentally is possible. I've tried simply adding the generated node.js code from gRPC in to no avail.

The last answer I got was from last year here.

4 Answers

I'm not sure whether I should duplicate my own answer, so, please take a look at a potential pure-JavaScript solution at:

Implement gRPC in an iOS React Native app

Maybe the above could ease having to link native code and still be cross-platform.

At the time of writing, Improbable Engineering's gRP-Web can't be used with React Native.

Instead you must use React Native Native Modules and build native modules in Java, Kotlin, Objective-C, and/or Swift.

You will:

  1. implement a gRPC client in the native language of your device,
  2. export it using React Native's Native module framework
  3. access the native module and its gRPC methods through React Native
  4. Call those exported methods in React your Native code

If, for example you are building on iOS/Swift, you'll need to do:

Reat Native <-> ObjC Bridge <-> Swift <-> gRPC

On Android it will be like this:

React Native <-> Android Java

I've written a tutorial for Building a gPRC Client in React Native on iOS. I hope it is helpful.

And one for Writing a native module for React Native using Kotlin

iOS/Objective-C and Android/Java are covered by the React Native documentation

Related