I added the check_origin: false
to the endpoint.ex file:
socket("/live", Phoenix.LiveView.Socket,
websocket: [connect_info: [session: @session_options], check_origin: false]
)
And in runtime.exs, I set up the right values:
host = System.get_env("PHX_HOST") || "localhost"
port = String.to_integer(System.get_env("PORT") || "4000")
config :magnetissimo, MagnetissimoWeb.Endpoint,
url: [host: host, port: port, scheme: "http"],
http: [
# Enable IPv6 and bind on all interfaces.
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
ip: {0, 0, 0, 0, 0, 0, 0, 0},
port: port
],
secret_key_base: secret_key_base
When I run docker compose up
and access localhost:4000
I see this error:
With this error message in my console:
04:39:49.235 [error] Could not check origin for Phoenix.Socket transport.
magnetissimo |
magnetissimo | Origin of the request: http://localhost:4000
magnetissimo |
magnetissimo | This happens when you are attempting a socket connection to
magnetissimo | a different host than the one configured in your config/
magnetissimo | files. For example, in development the host is configured
magnetissimo | to “localhost” but you may be trying to access it from
magnetissimo | “127.0.0.1”. To fix this issue, you may either:
magnetissimo |
magnetissimo | 1. update [url: [host: …]] to your actual host in the
magnetissimo | config file for your current environment (recommended)
magnetissimo |
magnetissimo | 2. pass the :check_origin option when configuring your
magnetissimo | endpoint or when configuring the transport in your
magnetissimo | UserSocket module, explicitly outlining which origins
magnetissimo | are allowed:
magnetissimo |
magnetissimo | check_origin: [“https://example.com”,
magnetissimo | “//another.com:888”, “//other.com”]
Typically in prod environments I know the domain of the where my app would be running and setting the host is enough. In this case I just want it to run on localhost:4000 while it’s MIX_ENV=prod.