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

  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Passwordless Image Pulls in Kubernetes With ServiceAccount Tokens and a Credential Provider
  • Zero Trust for Agents: A New Lens to Secure Agents
  • Reusable Operational Playbooks With Agent Skills

Trending

  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • Spring Security JWT Validation Best Practices
  • Your Event Storming Is Not Your Software Design
  • Why More Executors Don't Always Make Spark Faster: Understanding AQE

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)
13.0K 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

  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Passwordless Image Pulls in Kubernetes With ServiceAccount Tokens and a Credential Provider
  • Zero Trust for Agents: A New Lens to Secure Agents
  • Reusable Operational Playbooks With Agent Skills

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