<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="96664" data-post-id="96664">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="amclain" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/amclain/120/37691_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  amclain
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>Please, do you know any tutorial on implementing APIs with Cowboy or Plug?</p>
</blockquote>
<p>I recommend taking a look at the readme for Plug. The <a href="https://github.com/elixir-plug/plug#plugrouter" rel="noopener nofollow ugc"><code>Plug.Router</code></a> section shows how to create route handlers for your API – it’s incredibly simple. If you have an application large enough to need to keep the business logic out of the routes (basically any non-trivial app), just create a <code>models</code> directory, put that logic in there, and use the route handlers similar to how you would use a controller in an MVC framework: control access, validate parameters, make calls to models.</p>
<p>One trick I like to do that’s not in the Plug docs is to create helpers for the HTTP responses:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Routes.Helpers do
  import Plug.Conn

  def ok!(conn, body) do
    conn
    |&gt; send_resp(200, body)
    |&gt; halt
  end

  def not_found!(conn) do
    conn |&gt; error!(404, "Not Found")
  end
end
</code></pre>
<p>That way you can respond to a route using code that’s a little easier to read in my opinion. It’s also a good place to handle JSON encoding if your API produces JSON responses (not shown here).</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">get "/hello" do
  conn |&gt; ok!("world")
end

match _ do
  conn |&gt; not_found!
end
</code></pre>
<p>That’s just my personal preference though. Do what makes sense for your project.</p>
<p>Regarding Cowboy, there’s a pretty extensive <a href="https://ninenines.eu/docs/en/cowboy/2.3/guide/" rel="noopener nofollow ugc">Cowboy User Guide</a>. Just keep in mind that Cowboy is implemented in Erlang, so you’ll have to do some mental conversion from Erlang to Elixir when interfacing with it. It’s not too difficult, but you may want to be comfortable with the basics of Elixir first. Plug is probably going to be the easier of the two to get started with. Plug uses Cowboy under the hood in its typical configuration (and Phoenix uses Plug). An advantage to building directly on Cowboy is if you’re concerned about keeping your dependencies to a bare minimum (security requirements, maintainability, simplicity, etc.).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="96664" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/must-i-use-phoenix-framework/16109/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-96664" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="96664"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="96669" data-post-id="96669">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="4dcoder" data-post="30" data-topic="16109">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/4dcoder/48/11491_2.png" class="avatar"> 4dcoder:</div>
<blockquote>
<p>Sir, is there a tutorial/doc for UDP with cowboy? Thanks</p>
</blockquote>
</aside>
<p>Well if you don’t need the extreme performance that ranch is built for then just using gen_udp that comes with the beam is perfectly fine, a good tutorial is at:  <a href="https://learnyousomeerlang.com/buckets-of-sockets" class="inline-onebox" rel="noopener nofollow ugc">Buckets of Sockets | Learn You Some Erlang for Great Good!</a></p>
<p>As for ranch, I’m unsure if a udp version of it is built currently, I could have sworn there was at one point years ago but I can’t recall the name of it if so…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="96669" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/must-i-use-phoenix-framework/16109/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-96669" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="96669"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #32"></div>
  </section>
</div>
    <div class="postbit" id="96898" data-post-id="96898">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="4dcoder" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/4dcoder/120/11491_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  4dcoder
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks a lot <span class="mention">@amaclain</span></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="96898" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/must-i-use-phoenix-framework/16109/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-96898" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="96898"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #33"></div>
  </section>
</div>
    <div class="postbit" id="96901" data-post-id="96901">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dgmcguire" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dgmcguire/120/12192_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dgmcguire
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I would revisit whether you even want to use UDP - there are plenty of scenarios where TCP is much faster than UDP and generally it’s the sane choice for building an API if you’re unsure about which one will fit your needs.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="96901" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/must-i-use-phoenix-framework/16109/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-96901" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="96901"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="97215" data-post-id="97215">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="4dcoder" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/4dcoder/120/11491_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  4dcoder
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/dgmcguire" rel="nofollow">@dgmcguire</a> this is informative. Thank you Sir</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="97215" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/must-i-use-phoenix-framework/16109/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-97215" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="97215"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>