Need some help with Cowboy

This is probably something simple I don’t know. I have been through two different Cowboy/Plug on Elixir tutorials and in each case, Cowboy won’t answer on port 4000. I just get connection refused, which is what one would expect when there is no server process running. I’m doing this on a mac and I have been able to get other servers to answer on non-standard ports. Thoughts?

Here are the tutorials:

http://www.dendeer.com/post/elixir-plug/

1 Like

Can you please provide the bits of config you are using?

1 Like

Well config.exs is empty but for comments and this line:

use Mix.Config

It was generated by Mix.

There does not appear to be anything in the code to specify a listening port and my understanding is that the default is 4000. I tried adding some config for the port in config.exs but all I got was syntax errors.

observer shows that cowboy and plug, among other things, are running.

1 Like

After you have started cowboy, what does netstat -lnpt show you?

1 Like

On Mac we use lsof and it shows no indication that there is a listener on port 4000, and does not list any process resembling iex or erlang or cowboy or ranch or plug.

1 Like

Here are some examples of using different versions of cowboy with elixir

elixir + cowboy2: https://github.com/idi-ot/which_is_the_fastest/tree/master/elixir/cowboy2

elixir + cowboy1 + plug: https://github.com/idi-ot/which_is_the_fastest/tree/master/elixir/plug

elixir + cowboy1: https://github.com/idi-ot/which_is_the_fastest/tree/master/elixir/cowboy1

Maybe there is something that might help you.

Note, that you don’t need plug for websockets, just cowboy.

1 Like

I don’t understand this dependency:

def deps do
[{:my_cowboy, “~> 0.1.0”}]
end

All the links you posted have something like that in the install instructions, but hex complains that there is no such package as my_???. I realize the actual package name probably does not start with my_ but I don’t see what the examples are getting at.

As for plug, I was just trying to get something working quick, following a tutorial. I think this has to be something simple/

1 Like

Oh, sorry, please, ignore these instructions, they are generated by mix for every project by default. In this case, :my_cowboy is just the name of the app.

What I wanted you to see was inside /lib directories. They include working apps for different “cowboys”.

Not only /lib, actually. You might also find mix.exs useful. It defines dependencies of the app.

1 Like

Ah okay, thanks. I’ll check that now. And this does not seem to be a Mac issue, I can start a python web server on port 4000 with no trouble.
Thank you for your help.

1 Like

Actually those tutorials (at least one of them anyhow) are a bit old, so maybe things have changed. Here is my mix.exs file, which seems to bring the dependancies in a private:

defmodule Plugger.Mixfile do
use Mix.Project

def project do
[
app: :plugger,
version: “0.1.0”,
elixir: “~> 1.5”,
start_permanent: Mix.env == :prod,
deps: deps()
]
end

Run “mix help compile.app” to learn about applications.

def application do
[
extra_applications: [:logger, :cowboy, :plug],
mod: {Plugger.Application, }
]
end

Run “mix help deps” to learn about dependencies.

defp deps do
[
{:cowboy, “~> 1.0.2”},
{:plug, “~> 1.0”}
# {:dep_from_hexpm, “~> 0.3.0”},
# {:dep_from_git, git: “https://github.com/elixir-lang/my_dep.git”, tag: “0.1.0”},
]
end
end

1 Like

Your mixfile seems fine to me. These versions of cowboy and plug should definitely work …

You don’t need to specify :extra_applications if they are already in deps, though (but it wouldn’t be a problem if you do).

I would change this mixfile to look like so

defmodule Plugger.Mixfile do
  use Mix.Project

  def project do
    [
      app: :plugger,
      version: "0.1.0",
      elixir: "~> 1.5",
      start_permanent: Mix.env == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger],
      mod: {Plugger.Application, []}
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:cowboy, "~> 1.0"}, # this allows for cowboy to be 1.1
      {:plug, "~> 1.0"}
    ]
  end
end
1 Like

Okay thank you again. I’m trying to get it to work using examples in the links you posted.

1 Like

I got it working. I just gave up, blew away what I had and started from scratch. It worked the first time through. I’ll never know why it didn’t work before but I can live with that.

2 Likes