LiveView and Multiple Endpoints


I came across the following thread while working to accomplish something similar:

I’m hoping to deploy an application that will serve up different endpoints for different hosts; eg, a request to will be routed to FooWeb.Endpoint, and a request to to BarWeb.Endpoint.

The routing part is easy. It does, though, break LiveView somehow—I get a 404 on the socket. Chris mentions something about “need[ing] to specify the socket declarations” on the child proxy application, but I’m not sure exactly what that means. I’m wondering if he, or someone else, could expound upon that!


1 Like

Your proxy endpoint will need to have a socket declaration with matching session information each of Plug.Session of the proxied endpoints:

  @proxy_endpoints [foo: FooWeb.Endpiont, bar: BarWeb.Endpiont]
  for {app, endpoint} <- @proxy_endpoints do
    @app app
    @session Application.compile_env!(app, endpoint)[:session]
    socket "/#{@app}/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session]]

And your child endpoints can reference their session lookup from the same application config:

defmodule FooWeb.Endpoint do
  @session_options Application.compile_env!(:foo, FooWeb.Endpoint)[:session]
  plug Plug.Session, @session_options

Make sense?


That does! I think I was turned around on it because my initial proxy endpoint was only utilizing plug_cowboy, so I just wasn’t thinking about the socket/3 macro.

This is great, Chris! Thanks so much!