Phoenix sub app live socket not routing

My umbrella app routes calls through a “master proxy” which really is a big plug that has a bunch of redirections. Sample:

defmodule MasterProxy.Plug do
  alias MasterProxy.Plug.Request

  def init(options) do
    options
  end

  def call(conn, _opts) do
    cond do
        conn.request_path =~ ~r{/my_app} ->
          MyApp.Endpoint.call(conn, [])
.....
    end
  end
end

One of these redirects is towards a phoenix sub-app and it works fine except for the the live socket (used for phoenix live views).
It complains that the route for /live/websocket was not found even though my endpoint.ex contains:
socket "/live", Phoenix.LiveView.Socket as per the doc.
Now the weird part is that if I hit the sub-app directly on it’s port (say 4008 locally) then the socket seems to connect fine.
My guess is that something goes awry when it passes through the proxy redirect, but I can’t seem to figure out what?
Any clues?

For anybody finding this post and wondering the same thing, it seems like sockets are directly wired into cowboy and are not handled by Plug. As a result, a master proxy approach cannot redirect a socket connection and the route will fail to be resolved.
It’s a limitation of this approach and afaik there is nothing you can do about it outside of routing to your sub-app directly and by-passing the master proxy.

2 Likes