Error with websocket dispatch config

I am trying upgrade to Elixir 1.8, Erlang 21, and Phoenix 1.4. I’m getting an error with our custom http dispatch config, which was added when we were at Phoenix 1.3/Elixir 1.5.

Here is our config:

  config :site, SiteWeb.Endpoint,
    http: [
      dispatch: [
           {"/socket/websocket", Phoenix.Endpoint.CowboyWebSocket,
            {Phoenix.Transports.WebSocket, {SiteWeb.Endpoint, SiteWeb.UserSocket, :websocket}}},
           {"/socket/longpoll", Plug.Cowboy.Handler,
            {Phoenix.Transports.LongPoll, {SiteWeb.Endpoint, SiteWeb.UserSocket, :longpoll}}},
           {:_, Plug.Cowboy.Handler, {SiteWeb.Endpoint, []}}

And the error we’re seeing:

** (UndefinedFunctionError) function Phoenix.Endpoint.CowboyWebSocket.init/2 is undefined or private
    (phoenix) Phoenix.Endpoint.CowboyWebSocket.init(%{bindings: %{}, body_length: 0, cert: :undefined, has_body: false, headers: %{"accept-encoding" => "gzip, deflate, br", "accept-language" => "en-US,en;q=0.9", "cache-control" => "no-cache", "connection" => "upgrade", "cookie" => "_ga=GA1.2.323690242.1505750053; __insp_uid=4076678333; _ga=GA1.3.1382101712.1505318427;`AQKVD`5182813288529920:5629499534213120; __insp_wid=101978311; __insp_nv=false; __insp_targlpu=aHR0cHM6Ly9kZXYubWJ0YWNlLmNvbS8%3D; __insp_targlpt=TUJUQSAtIE1hc3NhY2h1c2V0dHMgQmF5IFRyYW5zcG9ydGF0aW9uIEF1dGhvcml0eQ%3D%3D; __insp_norec_sess=true; __insp_slim=1525973426869; mbta_id=32820746; _ga=GA1.4.323690242.1505750053; _site_key=SFMyNTY.g3QAAAAA.xxxBgGh7d8xGoy6cezO2La-1xvwRbt_RjIPutJlQj8U; _gid=GA1.3.930802726.1551714745; _gid=GA1.2.930802726.1551714745; mbta_visited_routes=CR-Fitchburg|85; _gid=GA1.4.930802726.1551714745; _gat_UA-82280837-1=1", "host" => "", "origin" => "", "pragma" => "no-cache", "sec-websocket-extensions" => "permessage-deflate; client_max_window_bits", "sec-websocket-key" => "IdIkX8mVrmEKVbPIdL0ZZw==", "sec-websocket-version" => "13", "upgrade" => "websocket", "user-agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36", "x-amzn-trace-id" => "Root=1-5c7eece1-4c47339fb70d03ccbdd1d686", "x-forwarded-for" => ",", "x-forwarded-port" => "443", "x-forwarded-proto" => "https", "x-real-ip" => ""}, host: "", host_info: :undefined, method: "GET", path: "/socket/websocket", path_info: :undefined, peer: {{172, 17, 0, 1}, 54138}, pid: #PID<0.6326.0>, port: 80, qs: "vsn=2.0.0", ref: SiteWeb.Endpoint.HTTP, scheme: "http", sock: {{172, 17, 0, 2}, 4000}, streamid: 9, version: :"HTTP/1.1"}, {Phoenix.Transports.WebSocket, {SiteWeb.Endpoint, SiteWeb.UserSocket, :websocket}})
    (cowboy) /root/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
    (cowboy) /root/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
    (cowboy) /root/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Initial Call: :cowboy_stream_h.request_process/3
Ancestors: [#PID<0.6326.0>, #PID<0.2391.0>, #PID<0.2390.0>, SiteWeb.Endpoint, Site.Supervisor, #PID<0.2377.0>]

As far as I can tell, Phoenix.Endpoint.CowboyWebSocket hasn’t changed in a while, so I’m guessing that there was some change to how the args are being passed to that init function? I’m struggling to follow the pipeline to even figure out how this is getting called. If anyone has any suggestions they would be greatly appreciated!

1 Like

I think you’re just using an old adapter. Phoenix.Endpoint.CowboyWebSocket appears to be for cowboy 1. If you upgraded to cowboy 2, I think you want to follow this:

1 Like

Out of curiosity, why are you using this feature? It appears you’re just passing what phoenix would pass for you by default. If you’re not doing anything custom, you can probably just delete that config.

After combing through the commits in the socket transport refactor in Phoenix 1.4 much more carefully, I believe I have solved my problem – I thought i would document it here in case anyone else comes across this same issue.

With the new socket transport, sockets do not appear to need their own dispatch transports; Phoenix.Endpoint.Cowboy2Handlers takes care of everything. So I was able to replace the above config with simply:

http: [
  dispatch: [
    {:_, [
      {:_, Phoenix.Endpoint.Cowboy2Handler, {SiteWeb.Endpoint, []}}