DEV Community

Balaji D
Balaji D

Posted on

Need an help with implementation of ZIO-GRPC

Also refer this stack overflow: https://stackoverflow.com/questions/79426489/need-help-implementing-grpc-pub-sub-using-zio
I am able to connect GRPC server using scala and fetchResponse by calling the subscribe api using grpc StreamObserver. With the same configuration I tried using ZIO framework I am not reciving the response.

I have below configuration host: api.salesforce.pubsub.com port : 7443 tenantId : "xxx" accessToken : "yyy" instanceUrl : "https://zzz.com"

I tried with this configuration to build ManagedChannel and call subscribe Api by passing FetchRequest(topicName) i am successfully getting the FetchResponse using StreamObserver.

With the same configuration I am not able to receive the FetchResponse and not even getting an error. Below is the ZIO-GRPC implementation.

object Main extends ZIOAppDefault{
def managedChannel():ZManagedChannel={
val managedChannelBuilder: ManagedChannelBuilder[_] = ManagedChannelBuilder.forAddress(host,port)

val interceptor = Seq(ZClientInterceptor.headersReplacer{
  (_,_)=> SafeMetadata.make(
       ("tenantid",tenantId),
       ("accesstoken",accessToken),
       ("instanceurl",URI(instanceUrl).resolve("/").toString))
  })
Enter fullscreen mode Exit fullscreen mode

ZManagedChannel(managedChannelBuilder, interceptor)
}

def requestBody() ={
val fetchRequest: zio.stream.Stream[StatusException,FetchRequest]=ZStream.succeed(FetchRequest(topicName= "topic", replayPresent = EARLIEST))

fetchRequest
Enter fullscreen mode Exit fullscreen mode

}

def subcribeAndFetch():ZStream[PubSubClient, StatusException,Option[FetchResponse]]=
for{
_ <- ZStream.logInfo(s"fetching..")
response <- PubSubClient.subscribe(requestBody()).map(response => Some(response))
.catchAll(ex => ZStream.logError(s"the subscribe request failed: ${ex.getStatus}").as(None))
} yield response

override def run ={
subcribeAndFetch
.runDrain
.repeat(Schedule.spaced(2.second))
.provideLayer(PubSubClient.live(managedChannel()))
}
}

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay