<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Otun Adeola on Medium]]></title>
        <description><![CDATA[Stories by Otun Adeola on Medium]]></description>
        <link>https://medium.com/@readonegoke?source=rss-8a2f0a02a77d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jZOmWHe1FXKe_p-S.jpg</url>
            <title>Stories by Otun Adeola on Medium</title>
            <link>https://medium.com/@readonegoke?source=rss-8a2f0a02a77d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 03:28:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@readonegoke/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What’s in the plug? : a look into Elixir’s Plug library.]]></title>
            <link>https://medium.com/the-plug/whats-in-the-plug-a-look-into-elixir-s-plug-library-7fd8fb856c0a?source=rss-8a2f0a02a77d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fd8fb856c0a</guid>
            <category><![CDATA[plug]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Otun Adeola]]></dc:creator>
            <pubDate>Tue, 08 Jan 2019 11:46:43 GMT</pubDate>
            <atom:updated>2019-01-08T12:03:32.947Z</atom:updated>
            <content:encoded><![CDATA[<p>What’s in the plug? : a look into Elixir’s Plug library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*5ebCzeQ68GvEzYc9NrXbFQ.jpeg" /><figcaption>How Phoenix uses the Plug library. image credit:Yurii Bodarev</figcaption></figure><p><strong>Plugs</strong> are integral parts of Elixir’s HTTP layer. <strong>While not part of Elixir core, It’s an official Elixir project.</strong> The request and response cycle in an Elixir/Phoenix application is effortlessly and efficiently handled by Plugs. For the developers familiar with the Ruby programming language, you’d notice that Plug is just like “<strong>Rack + Sinatra</strong>”. You’ll see for yourself in a bit.</p><p>According to Phoenix docs,</p><blockquote>Plug is a specification for composable modules in between web applications. It is also an abstraction layer for connection of adapters of different web server.</blockquote><p>Think of Plug as an HTTP middleware layer, whose basic idea is to unify the concept of a connection. On every request received, an initial <strong>%Plug.Conn{} </strong>struct (a connection) is established, and sent down to other Plugs in the pipeline. Each Plug in turn consumes this connection struct (perform tasks on the struct and refine it) and produces another connection struct. This struct has everything that web applications need; <strong>the inbound request, the protocols, parsed parameters and so on.</strong></p><p>The Plug library supports two (2) types of plug; <strong>module plug and function plug.</strong></p><h4>Module Plug</h4><p>Module Plugs are preferable when you require the Plug to be used or shared in multiple modules in your application.</p><p>Note: A module plug must implement two functions;<strong> </strong>init/1<strong> and </strong>call/2</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c41a8f87e80db30c46e5f96ecc94fc1/href">https://medium.com/media/8c41a8f87e80db30c46e5f96ecc94fc1/href</a></iframe><p>One interesting characteristic of module plugs is that init/1 is executed in compile time, while call/2 happens at run time. The value returned by init/1 will be passed to call/2, making init/1 the perfect place to do any heavy lifting and let call/2 run as fast as possible at run time.</p><p>To try to make things less abstract, let’s create a simple application that uses a plug to handle an http request.</p><p>First, create a project with mix:</p><pre>$ mix new demo_plug<br>$ cd demo_plug</pre><pre>//edit the mix.exs file to add plug and cowboy.<br>defp deps do<br>  [{:plug, &quot;~&gt; 1.0&quot;},<br>   {:cowboy, &quot;~&gt; 1.0&quot;}]<br>end</pre><p>Now run mix deps.get to install these dependencies.</p><p>Our first plug would output Hello world, welcome home.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2af56f28de5b6f4f5cb46fa4a7605516/href">https://medium.com/media/2af56f28de5b6f4f5cb46fa4a7605516/href</a></iframe><p>To use this plug, open iex -S mix and run:</p><pre>Plug.Adapters.Cowboy.http(LearningPlug, %{})<br>{:ok, #PID&lt;0.157.0&gt;}</pre><p>Here we use the Cowboy adapter, and tell it to use our plug. We also need to pass an opts value that will be used by init/1.<br> This should have started a Cowboy web server on port 4000, so if you open http://localhost:4000 you should see the “Hello, World, welcome home” message.</p><p>in the next post, we would talk about function Plugs, Pipelines and the Phoenix routes file.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fd8fb856c0a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-plug/whats-in-the-plug-a-look-into-elixir-s-plug-library-7fd8fb856c0a">What’s in the plug? : a look into Elixir’s Plug library.</a> was originally published in <a href="https://medium.com/the-plug">The Plug</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>