Originally published on HackerNoon
Robots work in all kinds of places with varying network conditions, from stable ethernet to inconsistent cellular or satellite connections. Learn how you can use gRPC and WebRTC for robots and other smart machines to communicate more reliably.
Why use gRPC AND WebRTC for controlling robots?
I recently started a new job at Viam, and was curious to learn more about their system architecture. Many robotics systems are composed of low-power and low-bandwidth devices, or operate in poor network conditions. Viam is an open-source robotics software platform that was designed specifically to help robots and other smart machines communicate more reliably.
It uses gRPC for fast, structured client-server communication and WebRTC for direct, peer-to-peer communication between machines. Let’s explore the benefits of using gRPC and WebRTC in more detail.
Benefits of using gRPC and Protobuf in robotics
There’s a number of reasons to use gRPC for robotics, such as for improved developer productivity and developer experience. However, the most compelling reason is that it is optimized for speed, perhaps up to 7–10x faster than REST APIs over HTTP, with lightweight Protobuf messaging.
Performance and efficiency
- Encoding: Protobuf offers efficient binary encoding, ideal for machines with low power, memory, compute, or bandwidth constraints compared to JSON-RPC.
- Transport: Along with the default transport of HTTP/2, gRPC supports alternative transports like HTTP/1.1 with gRPC-Web and WebRTC. This provides a consistent interface with the flexibility of underlying transport technology, especially with streaming.
Components and services in the Viam API are represented as language-agnostic Protocol Buffer (protobuf) services, and gRPC is responsible for the transport and communication of protobuf messages when calling protobuf methods.
Specifically, Viam uses gRPC for high-level interactions between machine parts and SDK-based applications, enabling structured, low-latency data exchange and control commands. This is an efficient way to coordinate work to be done, such as retrieving point clouds from cameras, analyzing data for object segmentation, and instructing robotic arms to perform tasks based on computed poses.
Protobuf minimizes the size of the transmitted data, saving network bandwidth during transmission.
Benefits of using WebRTC in robotics
There’s a number of reasons to use WebRTC in robotics, such as for security and cross-platform compatibility. However, the most compelling reason is that it uses a direct connection.
Peer-to-peer communication
- Direct: WebRTC facilitates direct connections between devices, reducing reliance on central servers and minimizing the traversal route of data.
- Network adaptability: Adjusts media quality based on network conditions for smooth performance, such as for video streaming.
Viam relies on WebRTC for peer-to-peer communication, allowing direct video and data streams between machine parts and the Viam SDK. This means once the initial connection is established, your data doesn’t need to take the long way going through a central server on its way to talking to another machine. This setup optimizes real-time responsiveness and operational efficiency especially with complex machine configurations.
WebRTC enables direct peer-to-peer communication, avoiding intermediate servers that could introduce delays or points of failure.
How Viam uses gRPC and WebRTC to enable peer-to-peer robot communication
Let’s see how this works in the example of an arcade claw game composed of two machine parts: a camera and an arm.
The Viam app (app.viam.com) listens for connection requests, sends connection details, and then lets the two peers communicate directly. The initial connections are facilitated over gRPC.
Once connected, the machine parts communicate over WebRTC, allowing all interactions between SDKs and machines to happen over WebRTC using gRPC method calls. In other words, the gRPC schema decides what the data looks like and how it’s packaged, while WebRTC handles the actual delivery of that data from one place to another.
Get started with robotics using Viam
While you could build out a similar infrastructure on your own, Viam provides this capability with off-the-shelf robotics software. The on-machine functionality is open-source and free to operate. If you eventually start managing a fleet, it’s usage-based billing for cloud services and data storage.
The Viam web app and SDKs were designed to help you work with your machines. However, if you want to explore what’s happening behind-the-scenes, check out the Viam APIs public workspace in Postman for step-by-step instructions on working with the underlying Viam gRPC APIs, including:
- Authorizing gRPC API calls
- Invoking methods to control hardware components
- Invoking methods to manage software services
You may not have a robotic arm at your disposal, but you can try this out with a more affordable machine, like a rover, such as Yahboom or SCUTTLE. Viam also designed an open-source rover to help folks learn about robotics.
Use gRPC and WebRTC to operate a rover
To get started, fork the Spin a Viam rover collection to your own workspace by clicking the Run in Postman button below.
And follow along with these resources:
- Step-by-step codelab: Postman tutorial for Viam's gRPC APIs
- Browse the collection documentation for more details about each request
- Watch the video: Spin a rover with the Viam gRPC API
Technical review by: Nick Hehr