shijith.k

shijith.k

BEAM, Cowboy and Plug

How does BEAM work? What does cowboy do exactly? and what exactly is Plug?

I still don’t have a clarity between these things. Can someone explain it please?

Marked As Solved

hauleth

hauleth

BEAM is the acronym of Bogdan/Björn’s Erlang Virtual Machine and is one of the implementation of Erlang VM (others are JAM, Lumen, Enigma, ErlangRT, AtomVM). This one is most popular one, and is the Erlang VM (reference implementation). Think of it as an OpenJVM vs Dalvik/OracleJVM/etc. in Java world.

Cowboy is HTTP(S) and WebSocket server, think of it like Glassfish/Jetty/Tomcat in Java world, or WEBrick/Puma/Unicorn in Ruby world.

Plug is common interface for HTTP servers. This is more like Rack in Ruby universe.

So in short:

  • BEAM is VM
  • Cowboy is HTTP(S) server implementation
  • Plug is library for providing common API to different HTTP(S) servers (currently only server supported officially is Cowboy, but in theory you can use any other as long as you create adapter).
12
Post #2

Also Liked

Aetherus

Aetherus

A plug is just a function that accepts a Plug.Conn.t and a configuration “thing” (I just want to say “object” but Elixir is not object oriented so I don’t know how to name it. A “term”?) That function can be wrapped in a separate module (something like a functor if you are familiar with C++).

If a function accepts the same arguments as above, changes the conn a little bit, and delegates the job to a plug, then the function is another plug.

In this way, you can chain plugs together, with each plug handles only a small task in the whole HTTP request handling, and the result of the chaining is a big plug. That’s what “composable” means. For example, in a conventional Phoenix application, you can find Plug.RequestId which attaches a unique random string to each request for the DevOps team to trace every request; Plug.Parsers for parsing the query strings and request bodies; Plug.MethodOverride for overriding the method according to the _method param; Plug.Session for loading sessions, and finally the MyApp.Router for dispatching each request to the correct controller action. (I omitted a few plugs) These plugs combine together into a single big plug.

If an Erlang/Elixir web server can chew and spit Plug.Conn.t, it can run any plug-based web application such as Phoenix application. If it can’t, well, probably someone will write or has already written an adapter for that server. So you can think of Plug as an adapter between your web application and the plug-aware web server, and that’s what “Connection adapters” mean.

tiagonbotelho

tiagonbotelho

@shijith.k I’ve actually wrote a small deep-dive into Plug and Cowboy and how they work under the hood which you can read here hope it helps!

kokolegorille

kokolegorille

  • The BEAM is needed to run any Erlang/Elixir application.
  • Cowboy is an Erlang application… (It would be the A in LAMP)
  • Plug is just a library, that can be used by application.

Where Next?

Popular in Questions Top

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics 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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement