![Search icon](https://hackernoon.imgix.net/search-new.png?w=19&h=19)
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.
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.
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 optimized for speed, perhaps up to 7–10x faster than REST APIs over HTTP, with lightweight Protobuf messaging.
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.
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.
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.
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.
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 sample collection to your own workspace by clicking the Run in Postman button below.
And follow along with these resources:
Technical review by: Nick Hehr