nikody

nikody

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.

Showing Posts 1 to 5

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

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews