DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Trending

  • Why We Chose Iceberg Over Delta After Evaluating Both at Scale
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • 5 Proven Strategies for Modernizing Legacy .NET Applications

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Unary Streaming via gRPC

Unary Streaming via gRPC

By 
Munander Singh
·
May. 07, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

If you want to get multiple responses and send multiple requests, it is time to use gRPC Streaming concepts. You can't do streaming with REST API, as REST API uses HTTP 1.1. In this blog, I will go through how you can do Unary Streaming via gRPC.

Types of APIs or Streaming in gRPC

gRPC supports four types of APIs to support streaming.

  1. Unary API.
  2. Server API.
  3. Client API.
  4. Bi-directional API.

Types of gRPC streaming

Unary API

Unary RPCs where the client sends a single request to the server and gets a single response back, just like a normal function call.

Java
 




x


 
1
service GreetService {
2
    // Unary
3
    rpc Greet(GreetRequest) returns (GreetResponse) {};
4
}



GreetService code

When we compile this .proto file, the code will be generated for us and we will have to provide implementations on the client-side and the server-side for the generated API.

Now, we have to set up the server, as shown in the following picture:

Setting up the server

Now, we have to set the medium between server and client.

Setting medium between server and client

Here, 50051 is the port number in which the server is running and usePlaintest() for not providing any type of security.

Now, we have to set up the server and channel. We will provide implementations on the server-side and client-side. You will use stub created from the generated code to call the generated API. Here is the server-side implementation

GreetService implementation

The request to the server is sent through the managed channel using the synchronous stub. An asynchronous stub is used in case of client streaming and bidirectional streaming. Here is the client-side implementation:

Client-side implementation

Finally, you have to up the server by running the server code and send the request from the client. You will get the desired response.

Here is the source code https://github.com/Munandermaan/Streaming-via-grpc.

I will show how to do client, server and bi-directional streaming in my upcoming blogs.

Conclusion

After going through this blog, you will understand how to do Unary Streaming via gRPC in the easiest way.

Reference

https://grpc.io/docs/guides/concepts/.

API

Opinions expressed by DZone contributors are their own.

Related

  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook