bluejack
In looking do a simple, but potentially slow task after the response has been delivered to the end user, I am looking for some lifecycle hook to trigger once the response is over.
In googling this I see references to “register_before_send” – but in fact I want to execute this code after send.
I see some contradictory older threads that look somewhat complicated involving task and/or task supervisor, but it’s not clear those are relevant to the current state of phoenix.
So: what is the best practice for doing work after a response has been successfully sent?
I was imagining there would be a straightforward way to add a post-processing plug or a post-action callback.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
msmithstubbs
I’m not aware of away to do this with plug, but Phoenix does emit telemetry events. You could subscribe to a telemetry
[:phoenix, :endpoint, :stop]event and execute your task then.LostKobrakai
You can decide on your own when exactly in the plug pipeline you send the response (using
Plug.Conn.send_resp/1or any code calling it). E.g. in a phoenix app that would happen as part ofPhoenix.Controller.render/3. Sending a response doesn’t stop the plug pipeline though. You can have further plugs after that. You can even implement a similar callback registration toregister_before_sendif you want to.If you don’t manually call
Plug.Conn.send_respwill be called by plug at the end of a plug pipeline – expecting a response to be set on theconn.bluejack
Thanks for this tip; however, it appears to me that all plugs execute before the controller; so, I guess what I am going to try is having the controller call the send_resp, and then hand off to post response code.
This is a pretty typical setup for an api server with a router, in which the router opens with “pipe_through”
bluejack
Additionally, when calling send_resp directly, everything works from the client’s perspective, but the Phoenix app reports an error:
In the documentation for Conn send_resp, it notes that one should call halt/1, but neither doing this nor not doing this prevent the error.
LostKobrakai
You could plug a plug after the router (in endpoint.ex).
That error would suggest you did not call
send_resp(at all), so not sure what’s up with that.bluejack
Right, but the response is being delivered to the client.
Update – the answer to that particular one is: I wasn’t returning the modified new conn, which probably gets marked with “response sent” for downstream record keeping.
Nonetheless, let me redirect to putting the plug in endpoint.ex instead of the pipeline, which would be the more general solution if I can make it work!
bluejack
Final note: this IS the answer –
Adding the plug in endpoint.ex after the router.
This was way easier than I feared… and way harder to figure out than I expected. But I am still pretty new to Phoenix.