This guide gets you started with gRPC in LabVIEW with a simple working example.
- LabVIEW 2019 or higher
- VIPM 2020 or higher
To install gRPC:
- Download the latest package from Releases.
- Download and Unzip
grpc-labview.zip
which contains the released packages.
- Download and Unzip
- Install the following gRPC package,
ni_lib_labview_grpc_library-x.x.x.x.vip
LabVIEW gRPC tools include the server base libraries and scripting tool to generate the server and client code from '.proto' service definitions. For the first part of our quick-start example, we have already generated the server and client code from helloworld.proto, but you will need the tools for the rest of our quick start, as well as for your own project.
To install gRPC tools:
Note: Install LabVIEW gRPC from above, before continuing with below steps
- Install the following tool packages,
ni_lib_labview_grpc_servicer-x.x.x.x.vip
ni_lib_grpc_server_and_client_template[2]-x.x.x.x.vip
To download the example:
You will need a local copy of the example code to work through this quick start. Download the example code from our GitHub repository (the following command clones the entire repository, but you just need the examples for this quick start and other tutorials):
# clone the repository to get the example code:
$ git clone https://github.com/ni/grpc-labview.git
# Navigate to the "hello, world" LabVIEW example using file explorer:
/grpc-labview/examples/helloworld/
To run the server and client:
From the examples/helloworld
directory:
-
Open
helloworld.lvproj
-
Run the server
- Run
Run greeter_server.vi
from helloworld.lvproj/helloworld_server/helloworld_server.lvlib/
or from file explorer/helloworld_server/
- Run
-
Run the client
- Run
Run greeter_client.vi
from helloworld.lvproj/helloworld_client/helloworld_client.lvlib/
or from file explorer/helloworld_client/
- Run
Congratulations! You have just run a client-server application with gRPC.
Note: This LabVIEW server and client example can run with Python client and server respectively.
Now let’s look at how to update the application with an extra method on the server for the client to call.
Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto
file in Introduction to gRPC. For now all you need to know is that both the server and the client code have a SayHello
RPC method that takes a HelloRequest
parameter from the client and returns a HelloReply
from the server, and that this method is defined like this:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Let’s update this so that the Greeter
service has two methods. Edit examples/protos/helloworld.proto
and update it with a new SayHelloAgain
method, with the same request and response types:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Remember to save the file!
Next we need to update the gRPC code used by our application to use the new service definition.
- Open
gRPC Template Creation Utility
fromLabVIEW -> Tools Menu -> gRPC -> Open gRPC Server-Client [2]-Code Generator
-
Configure the Code Generator as below,
- Proto File Path: File path of the updated helloworld.proto from
examples/protos/helloworld.proto
- Target Project: Project File Path of the example application from
examples/helloworld/helloworld.lvproj
- Generated Library Name:
helloworld
- Generate:
Both
- Proto File Path: File path of the updated helloworld.proto from
-
Run the Code Generator
This regenerates helloworld.lvproj
which contains the helloworld server and client code with new a new service method.
Note: In case, Client Code is not fully regenerated (as the tool is in beta version), delete the already created client code from the project and file explorer, and generate from scratch again
We now have new generated server and client code, but we still need to implement and call the new method in the human-written parts of our example application.
From the examples/helloworld
directory:
-
Open
helloworld.lvproj
-
Open
Start Sync.vi
from helloworld.lvproj/helloworld_server/helloworld_server.lvlib/Greeter.lvclass/Server API/
This is where the human-written parts of the application is present.
- In the EventStructure, goto the case called
helloworld_Greeter_SayHelloAgain gRPC UE
(If not present, create the case) and implement the new method like this:
- Open
Run Service.vi
from helloworld.lvproj
Note that before regeneration this was named Run greeter_server.vi
.
- Replace the current Greeter.lvclass constant with the new constant from project
- Delete old reference and and connect new reference
Althougth old reference looks the same it needs to replaced. If not replaced the VIs will immediately exit after execution without error in Run Service.vi
.
From the same project,
-
Open
Run greeter_client.vi
from helloworld.lvproj/helloworld_client/helloworld_client.lvlib/
-
Call the new method like this:
Just like we did before, from the examples/helloworld
directory:
-
Open
helloworld.lvproj
-
Run the server
- Run
Run greeter_server.vi
from helloworld.lvproj/helloworld_server/helloworld_server.lvlib/
or from file explorer/helloworld_server/
- Run
-
Run the client
- Run
Run greeter_client.vi
from helloworld.lvproj/helloworld_client/helloworld_client.lvlib/
or from file explorer/helloworld_client/
- Run
- Learn how gRPC works in Introduction to gRPC and Core concepts.
- Work through the Basic tutorial.
- Explore the API reference.