Using Cowboy2 WebSocket handlers and Phoenix Channels on the same application

Does anyone know if it is possible to use Phoenix Channels and Cowboy WebSockets on the same application?
It looks like that adding http: [dispatch: [...] to my config/config.exs breaks phoenix channels.

After that change now I always get (Phoenix.Router.NoRouteError) no route found for GET /socket/websocket.
My dispatch code is the following:

  http: [dispatch: [
    {:_, [
      {"/mysock", MyWebsocketApp.SocketHandler, []},
      {:_, Plug.Cowboy.Handler, {Astarte.DeviceForwarderWeb.Endpoint, []}}
    ]}
  ]]

lib/astarte_device_forwarder_web/endpoint.ex:

socket "/socket", Astarte.DeviceForwarderWeb.UserSocket,
  websocket: [connect_info: [:peer_data, :x_headers]],
  longpoll: false
1 Like

I have this working

    dispatch: [
      {:_,
       [
         {"/ws", MyAppWeb.Websocket, []},
         {:_, Phoenix.Endpoint.Cowboy2Handler, {MyAppWeb.Endpoint, []}}
       ]}
    ]

I guess the difference is Plug.Cowboy.Handler vs Phoenix.Endpoint.Cowboy2Handler.

Hopefully this works for you.

1 Like

Thanks, it works :slight_smile: