ruevaughn

ruevaughn

Hi everyone, I am very new to the Erlang / Elixir / Phoenix scene, so sorry if i’m posting this in the wrong spot, or my terminology is off, feel free to correct me on anything since i’d rather learn.

I’m currently reading and following along with the “Programming Phoenix” book and have a few question about module plugs.

So far, what I understand is that for a module plug to be a ‘module plug’, it needs two functions, init/1 and call/2.

In the book, we make a module called Rumbl.Auth. This module uses the expected init/1 and call/2 functions, but also adds others, such as login/2, login_by_username_and_pass/4 and some others.

Are these ‘other’ functions (login, and login_by_username_and_pass) relying on call/2 in order to operate properly? Or are they completely isolated and ran independently?

Here is the Plug i’m working on… auth module plug

My question is due to me trying to understand when ‘call’ is actually being executed by Phoenix.

I understand I add the module to my router like so

defmodule Rumbl.Router do
use Rumbl.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug Rumbl.Auth, repo: Rumbl.Repo
  end

But i’m not sure when ‘call’ is actually getting called since I never explicitly call it. Is it getting called every time I run another function within that module? Or does it get called once? I understand init gets called during compile time to set things up for call, and call/2 gets executed at runtime.

I guess i’m confused why ‘call’ is even needed in this case, and why I can’t just treat the module (other than the fact that it needs call/2 to be considered a plug) as a module. Is call there just for the sake of being called a ‘plug’?

I hope my question makes sense. Sorry if it’s something blatant i’m missing. Excited to read any replies. Been really enjoying Functional / Erlang / Elixir / Phoenix so far… I haven’t been this excited to learn something in a long time.

Showing Posts 1 to 8

hubertlepicki

hubertlepicki

Phoenix actually never performs the “call” itself as far as I can tell. Phoenix is nothing more than a set of plugs and supporting libraries.

You are right about the init function, it gets called only when app stars. The call function, on the other hand, will be called on each request.

When a HTTP request comes in, Cowboy server is the first that starts handling such request. plug library adds hooks, builds up the chain of plug middlewares, and calls it, I believe here: plug/lib/plug/adapters/cowboy/handler.ex at 2df667389dbaebb0e2102343c8d7a6ed4de6e280 · elixir-plug/plug · GitHub

So, calling “call” happens on each and every request. Moreover, those calls get nested so a pipeline of plugs get processed by some macro (plug/lib/plug/builder.ex at main · elixir-plug/plug · GitHub) and when one call finishes, another call is executed, until some plug returns a valid response, in such case it is halted, response returned to the browser by cowboy. You don’t have to manually call upstream plugs because of smart macros that do it for us.

Basically, a request comes in → cowboy handler executed → plug1.call() → plug2.call() → plug3.call() etc.

bobbypriambodo

bobbypriambodo

You can think of this:

pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug :fetch_flash
  plug :protect_from_forgery
  plug :put_secure_browser_headers
  plug Rumbl.Auth, repo: Rumbl.Repo
end

will generate this:

conn # the initial conn handled by cowboy, passed on to Phoenix
|> accepts(["html"])
|> fetch_session
|> fetch_flash
|> protect_from_forgery
|> put_secure_browser_headers
|> Rumbl.Auth.call(repo: Rumbl.Repo)

Any request that goes to your app will be run through that pipeline first. You don’t have to explicitly call call/2.

Edit: actually my explanation is missing some details. Before going through the pipeline, the connection from first arrives at the Endpoint and went through a series of Plugs before going to the Router and, subsequently, the pipeline.

Does that make sense?

OvermindDL1

OvermindDL1

Actually closer to this (still not perfectly accurate, but keeps the init in this idea):

conn # the initial conn handled by cowboy, passed on to Phoenix
|> accepts(["html"]))
|> fetch_session()
|> fetch_flash()
|> protect_from_forgery()
|> put_secure_browser_headers()
|> Rumbl.Auth.call(Rumbl.Auth.init(repo: Rumbl.Repo))

But yeah, it is the ‘pipeline’ call that does the plugs there. In something like Endpoint the plugs are run via the plug builder that builds the ‘state’. :slight_smile:

bobbypriambodo

bobbypriambodo

And to answer this:

Those other functions are pure functions. They don’t depend on call/2 whatsoever and, as you said, are isolated from one another.

It might be nice to remember that even though Rumbl.Auth is a module plug, it is also a module. In Elixir (and many other FP languages), a module is usually just a collection of functions.

The login/2, login_by_username_and_pass/4, etc. are plain functions. Although login/2 fulfils the contract of a function plug, it is not used as such. They are called with Rumbl.Auth.login(user), and so on.

The reason that those functions are in the Rumbl.Auth module is most likely because as the name implies, they deal with user authentication, therefore it makes sense to group them under an Auth module.

ruevaughn

ruevaughn OP

Lots of great info guys - thank you so much. It makes a lot more sense to me - modules plugs are still Elixir modules - so any other functions within them are isolated from init/1 and call/2 (correct?), but they’re still used to contain like functionality, such as authentication.

Along with that, the plug will get executed (call/2 init/1 on startup) on each and every request since I plugged it into the Browser Pipeline. That happens when Cowboy handles it as @hubertlepicki pointed out.

It’s all becoming a little clearer. Thanks guys, still learning as I read through your posts.

josevalim

josevalim

Creator of Elixir

A small correction: init is called when the app compiles.

ruevaughn

ruevaughn OP

Thanks Jose! Great book, much appreciated. Where do I go to submit any errors / suggestions I find?

josevalim

josevalim

Creator of Elixir
— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews