Undefined function is clearly defined

Erlang v26. Cowboy 2.12.0.

Error:

Inside chatroom_handler:init
=CRASH REPORT==== 23-Apr-2025::23:20:46.786362 ===
  crasher:
    initial call: cowboy_clear:connection_process/4
    pid: <0.216.0>
    registered_name: []
    exception error: undefined function chatroom_handler:websocket_handle/2
      in function  cowboy_websocket:handler_call/6 (/home/xxx/chat3/_build/default/lib/cowboy/src/cowboy_websocket.erl, line 528)
      in call from cowboy_http:loop/1 (/home/xxx/chat3/_build/default/lib/cowboy/src/cowboy_http.erl, line 253)

chatroom_handler.erl

-module(chatroom_handler).
-behavior(cowboy_websocket_handler).
-export([init/2]).
-export([websocket_init/1]).
-export([websocket_handle/2]).
-export([websocket_info/2]).
-export([terminate/3]).

init(Request, State) ->
  io:format("Here 1~n"), % appears in shell
  {cowboy_websocket, Request, State}.

websocket_init(State) ->
  io:format("Here 2~n"), % never appears in shell
  {ok, State}.

websocket_handle(Frame = {text, _}, State) ->
  {reply, Frame, State}.

websocket_info(Info, State) ->
  io:format("Here 3~n").

terminate(Reason, PartialRequest, State) ->
  ok.

So… what am I doing wrong?

Me complaining:

It’s literally crazy to me that something like Phoenix sits on top of Cowboy, yet the documentation for Cowboy is the way it is right now. Why is there not a single whole-file example in the official Cowboy doc? I can think of a reason…

Are you using rebar3? Seems like either chatroom_handler is not available to the BEAM or maybe compiled file is outdated. Can you also share the steps of how exactly you trigger the crash?

Few point about it:
1 - cowboy_websocket_handler behavior is not exist in Cowboy > 2.X because it was removed
2 - websocket_info should not return atom ok
3 - Read documentation Nine Nines: cowboy_websocket(3)
4 - See working examples cowboy/examples/websocket at 2.12.0 · ninenines/cowboy · GitHub

1 Like

Thank-you. The websocket example linked works out-of-the-box. Neat. Okay, I can move forward from here.

1 Like