larshei

larshei

GenServer/Process to receive gRPC Stream Data?

Hey everyone,

I want to receive data from a gRPC stream.

So far, I only wrote functions, played around in iex and was able to receive and decode data. It is time to “applicationify” that previous work and add lifecycle management/processes. But somehow, the gRPC behaviour seems different in GenServer compared to the iex session? :thinking:

I found a similar post in the forum: Elixir-grpc can't receive stream response. Unexpected when waiting for headers- which ends on

Looks like GRPC.Stub.recv can’t be used in GenServers :thinking:

But that seems weird to me.

Setup in iex

Basically, the stripped down version looks something like this:

  # wrapper around GRPC.Stub.connect() that handles auth etc
  {:ok, channel} = SalesforceEvents.Grpc.connect()

  grpc_stream = Eventbus.V1.PubSub.Stub.subscribe(channel)
  GRPC.Stub.send_request(grpc_stream, Messages.request_data())
  {:ok, data_stream} = GRPC.Stub.recv(grpc_stream, timeout: 60_000)
  
  Enum.each(data_stream, &IO.inspect/1)
# shell blocks, protobuf-decoded data is printed as it comes in

Setup with GenServer

Same as above, but in a GenServer.

Handling incoming stream data with Enum.each is spawned as a separate process:

@impl true
def init(args) do
  {:ok, channel} = SalesforceEvents.Grpc.connect() 

  grpc_stream = Eventbus.V1.PubSub.Stub.subscribe(channel)
  GRPC.Stub.send_request(grpc_stream, Messages.request_data())
  {:ok, data_stream} = GRPC.Stub.recv(grpc_stream, timeout: 60_000)

  receiver_process =
    spawn_link(fn -> Enum.each(data_stream, &IO.inspect/1) end)

  state = %{
    channel: channel,
    grpc_stream: grpc_stream,
    data_stream: data_stream,
    receiver_process: receiver_process
  }

  {:ok, state}
end

def handle_info(info, state) do
   Logger.debug(inspect info)
  {:noreply, state}
end

Expected Behaviour

Received gRPC messages are still printed to the console

Actual Behaviour

Instead of the messages being printed, the handle_info receives messages like the following:

08:45:41.824 [debug] {:gun_data, #PID<0.339.0>, #Reference<0.2404288951.1709178882.257606>, :nofin, <<0, 0, 0, 1, 198, 10, 145, 3, …>>}

Why does this behave differently (nothing is printed, messages are not decoded)?
How can I receive data from a gRPC stream within a process getting results similar to what I had in iex?

First Post!

larshei

larshei

Interestingly it works when I move

  grpc_stream = Eventbus.V1.PubSub.Stub.subscribe(channel)
  GRPC.Stub.send_request(grpc_stream, Messages.request_data())
  {:ok, data_stream} = GRPC.Stub.recv(grpc_stream, timeout: 60_000)

into the spawned process.

Do the subscribe and recv functions need to be called from the same process?

Why is that?

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement