You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Full featured integration library for React and gRPC-Web. Core functions include: packaging the generated proto messages and client stubs, a unified API of gRPC call methods that support Google's and Improbable's gRPC-web specs for unary, client streaming, server streaming and bi-directional streaming.
7
-
7
+
8
8
# Getting Started
9
9
## Install
10
10
```
11
11
npm install --save reactrpc
12
12
```
13
-
13
+
14
14
## 1. Define the Services
15
15
Create proto files as the schema for your Server and Client Stubs. It should define the gRPC call methods needed to communicate between the Server and Browser. These files will be used to give your components superpowers -- Remote Procedure Call (RPC) methods.
After the command runs successfully on your `[name of proto].proto` you should see two generated files `[name of proto]_pb.js` which contains the messages and `[name of proto]_grpc_web_pb.js` that contains the services:
97
-
97
+
98
98
For instance the `helloworld.proto` file will generate to:
99
99
- messages : `helloworld_pb.js`
100
100
- services : `helloworld_grpc_web_pb.js`
101
101
102
102
## For Improbable's implementation:
103
-
103
+
104
104
For the latest stable version of the ts-protoc-gen plugin:
105
-
105
+
106
106
```
107
107
npm install ts-protoc-gen
108
108
```
109
-
109
+
110
110
Download or install protoc (the protocol buffer compiler) for your platform from the github releases page or via a package manager (ie: brew, apt).
111
-
111
+
112
112
Download protoc from [here](https://github.com/protocolbuffers/protobuf/releases)
113
-
113
+
114
114
When you have both `protoc` and `ts-protoc-gen` installed, you can now run this command:
@@ -121,87 +121,87 @@ When you have both `protoc` and `ts-protoc-gen` installed, you can now run this
121
121
./proto/examplecom/library/book_service.proto
122
122
```
123
123
After the command runs successfully on your `[insert_name].proto` you should see two generated files `[insert_name]_pb.js` which contains the messages and `[insert_name]_pb_service.js` that contains the services:
124
-
124
+
125
125
For instance for the helloworld.proto you should see:
126
126
- messages : `book_service_pb.js`
127
127
- services : `book_service_pb_service.js`
128
-
129
-
128
+
129
+
130
130
## 3. Create proxy server
131
-
131
+
132
132
In order for gRPC-web to communicate with other gRPC servers, it requires a proxy server as a translation layer to convert between gRPC-web protobuffers and gRPC protobuffers. Links to examples on how to set those up can be found [here](https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld) (Envoy proxy) and [here](https://github.com/improbable-eng/grpc-web/tree/master/go/grpcwebproxy) (Improbable's proxy)*
133
-
133
+
134
134
>*Note: To enable bidirectional/client-side streaming you must use Improbable's spec and its proxy with websockets enabled
135
-
135
+
136
136
## 4. Set up React component
137
-
137
+
138
138
Require in the reactRPC library and protobuf files in your React JSX file. Run the build method with the following params: the message, the services and the URL to the proxy server endpoint.
Export the reactRPC component by passing it as an argument into the improbRPC wrapper as follows:
173
-
171
+
172
+
Export the improbRPC component by passing it as an argument into the improbRPC wrapper as follows:
173
+
174
174
```javascript
175
175
exportdefaultimprobRPC.wrapper(<your component>);
176
176
```
177
-
177
+
178
178
## 5. Define a message
179
-
179
+
180
180
We define a request message by creating an object with the keys as the message field along with a `msgType` property specifying a message that we set in the proto file. Here is an example of a `HelloRequest` message in the `helloworld.proto` file :
We define a function by listing its service and procedure calls on `this.props`. We then pass in the message we defined above, and an object with any metadata data required (learn more about metadata [here](https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md)). For unary calls a third parameter of a callback is required while streaming calls have built in event listeners.
0 commit comments