DEV Community

Discussion on: Implementing a peer-to-peer network in Elixir - Part 1: The Server

Collapse
 
hugecoderguy profile image
Christian Kreiling • Edited

Heyo! You've really helped me understand how to use ranch in Elixir, thanks a ton.

I have one suggestion for you about how you set up your port listening process. Instead of wrapping the Ranch listener in your own GenServer, you can instead use :ranch.child_spec/5 in your application module to setup the listener. Like so:

defmodule Network.Application do
  use Application

  def start(_type, _args) do
    children = [
      :ranch.child_spec(
        :network,
        :ranch_tcp, 
        [{:port, 8000}], 
        Network.Handler, 
        []
      )
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end

Now all you have to do is write the handler module :) Notice that :ranch.child_spec/5 takes the same arguments as :ranch.start_listener/5.