nikody

nikody OP

I’m testing server-side events with Plug’s send_chunked/2 and chunk/2 but I also receive data from a stream. Due to the latter detail I decided to implement the receiving of the streamed data in a GenServer, the request is made with HTTPoison and it is similar to what is mentioned in this StackOverflow post, although I receive the chunk data in a slightly different format (still a non-JSON string with a data key), and had to also implement a handle_info callback for messages with a %HTTPoison.AsyncEnd{} struct.

Other than that it’s working and the GenServer is started from my controller. So essentially I have a controller that receives a request, then spins up a GenServer that makes another request, receives streamed data and has to send the data parsed back to the original connection.

The issue comes when trying to send the data back from the GenServer. I tried passing the conn struct to the genserver and calling Plug.Conn.chunk/2 from it but it seems that Cowboy recognized that a separate process sends a messages, raises a warning and then the GenServer stops processing. The Cowboy warning raised is here in the Cowboy code: [warning] Received message #{message inspected} for unknown stream 1. (or another stream ID, but generally integers 1 to 3).

As Cowboy spawns a process for each request I assume it expects that responses should be sent from this process which is what is causing this issue. I’ll probably implement a receive loop without spawning a different process to handle this, but I was wondering if I’m not missing or misunderstanding something and if there isn’t a way to send a response with Plug (Cowboy) from a separate process, in this case a GenServer.

First 5 of 5 Posts Switch mode

dimitarvp

dimitarvp

This will likely sound uniformed, still I can’t help but wonder why do you need a separate process (a GenServer in your case) to fetch stuff from outside at all?

Sounds like the caller will still have to wait i.e. the request is blocking, right? If that’s true then I see no value in using one more process.

But if I haven’t understood correctly and the request is not blocking then you’ll likely have more luck with wrapping your solution around a Phoenix Channel.

nikody

nikody OP

The request is blocking, yes. And you’re probably right, I likely don’t need a GenServer, but I wanted to abstract as much as I can in a separate module and as it has to receive messages by instinct I started with a GenServer. What I think I’ll do is just wrap the receive loop and parsing of streams in a separate module and call it from my controller and it will be with one process as you say. But I was also just curious about this Cowboy detail.

dimitarvp

dimitarvp

You can still do that but the abstraction doesn’t need be on a process level, it can just be functions in a separate module that get called directly in the context of the web requests’ process.

I too wouldn’t want a 100-line pattern-matching clauses soup capturing the state machine of calling an external API right inside my Phoenix controller function so good job there, you are on the right path.

I might have been misreading various blog posts and news but it seems the writing is on the wall that Cowboy will eventually be abandoned in favor of Bandit. Cowboy has legitimate problems with scaling, pooling and data copying (if I remember correctly!) and Bandit aims to fix (part of) those.

nikody

nikody OP

Yeah in the end it’s definitely better and simpler.

That’s interesting, thanks for mentioning! I will keep an eye out and look into Bandit as well.

derek-zhou

derek-zhou

I’d recommend to spawn a process, send the data chunks back to the original process, and run a receive loop in the original process to catch the data and then send to the conn. Processes are cheap on the BEAM, and you would like to have some abstraction.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement