Question

I am trying to run the cowboy web socket example but I keep running into an error:

{error,
    {bad_return,
        {{erws_app,start,[normal,[]]},
         {'EXIT',
             {undef,
                 [{websocket_sup,start_link,[],[]},
                  {application_master,start_it_old,4,
                      [{file,"application_master.erl"},{line,269}]}]}}}}}

The erlang command I am using to start the application: erl -pa ebin/ -pa deps/*/ebin

then I start the required dependency applications using application:start(app).

I use rebar to compile the application

The code I am using:

erws_app.erl

-module(erws_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1, start_link/2]).

start(_Type, _Args) ->
        Dispatch = cowboy_router:compile([
                {'_', [
                        {"/", cowboy_static, {priv_file, websocket, "index.html"}},
                        {"/websocket", ws_handler, []},
                        {"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
                ]}
        ]),
        {ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
                [{env, [{dispatch, Dispatch}]}]),
        websocket_sup:start_link().     

start_link(_Type, _Args) ->
        Dispatch = cowboy_router:compile([
                {'_', [
                        {"/", cowboy_static, {priv_file, websocket, "index.html"}},
                        {"/websocket", ws_handler, []},
                        {"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
                ]}
        ]),
        {ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
                [{env, [{dispatch, Dispatch}]}]),
        websocket_sup:start_link().

stop(_State) ->
        ok.

erws_handler.erl

-module(erws_handler).
-export([init/3]).
-export([websocket_init/3]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
-export([websocket_terminate/3]).

init({tcp, http}, _Req, _Opts) ->
    {upgrade, protocol, cowboy_websocket}.

websocket_init(_TransportName, Req, _Opts) ->
    erlang:start_timer(1000, self(), <<"Hello!">>),
    {ok, Req, undefined_state}.

websocket_handle({text, Msg}, Req, State) ->
    {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
websocket_handle(_Data, Req, State) ->
    {ok, Req, State}.

websocket_info({timeout, _Ref, Msg}, Req, State) ->
    erlang:start_timer(1000, self(), <<"How' you doin'?">>),
    {reply, {text, Msg}, Req, State};
websocket_info(_Info, Req, State) ->
    {ok, Req, State}.

websocket_terminate(_Reason, _Req, _State) ->
    ok.

erws_sup.erl

-module(erws_sup).
-behaviour(supervisor).

%% API.
-export([start_link/0]).

%% supervisor.
-export([init/1]).

%% API.

-spec start_link() -> {ok, pid()}.
start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% supervisor.

init([]) ->
    Procs = [],
    {ok, {{one_for_one, 10, 10}, Procs}}.

erws.app.src

{application, erws, [
    {description, ""},
    {vsn, "1"},
    {registered, []},
    {applications, [kernel,stdlib,crypto,cowboy,compiler,lager,syntax_tools]},
    {mod, { erws_app, []}},
    {env, []}
]}.

rebar.config

{erl_opts, [{parse_transform, lager_transform}]}.
{lib_dirs,["deps"]}.
{sub_dirs, ["rel"]}.

{deps, [
    {'lager', ".*", {
        git, "https://github.com/basho/lager.git", "2.0.2"}
    },
    {'ranch', ".*", {
        git, "https://github.com/extend/ranch.git", "0.9.0"}
    },
    {'cowlib', ".*", {
        git, "https://github.com/extend/cowlib.git", "0.4.0"}
    },
    {'cowboy', ".*", {
        git, "https://github.com/extend/cowboy.git", "0.9.0"}
    }
]}.

Help to solve this error is most appreciated.

regards

Was it helpful?

Solution

The error message shows that the function erws_app:start/2 tries to call the function websocket_sup:start_link/0 but no such function exists.

You've named the supervisor module as erws_sup, so replace all websocket_sup:start_link() in erws_app.erl with erws_sup:start_link().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top