Keep getting no route found for GET /phoenix/live_reload/socket/websocket

I’m trying to create an API in Phoenix and this is how I’ve created my Phoenix app:

$ mix phx.new my_app --no-html --no-ecto --no-webpack --no-dashboard

However I keep getting this debug info after running the server

[info] GET /phoenix/live_reload/socket/websocket
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /phoenix/live_reload/socket/websocket (MyAppWeb.Router)
    (my_app 0.1.0) lib/phoenix/router.ex:402: MyAppWeb.Router.call/2
    (my_app 0.1.0) lib/my_app_web/endpoint.ex:1: MyAppWeb.Endpoint.plug_builder_call/2
    (my_app 0.1.0) lib/plug/debugger.ex:136: MyAppWeb.Endpoint."call (overridable 3)"/2
    (my_app 0.1.0) lib/my_app_web/endpoint.ex:1: MyAppWeb.Endpoint.call/2
    (phoenix 1.5.9) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
    (cowboy 2.9.0) /Users/burak/git/my_app/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
    (cowboy 2.9.0) /Users/burak/git/my_app/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
    (cowboy 2.9.0) /Users/burak/git/my_app/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
    (stdlib 3.14.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

How can I stop this?

This is my endpoint.ex, not sure whether that is related to this file anyway, just pasting.
Let me know if you need to see any other specific file.

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  @session_options [
    store: :cookie,
    key: "_my_app_key",
    signing_salt: "ZEYFSyPp"
  ]

  # socket "/socket", MyAppWeb.UserSocket,
  #   websocket: true,
  #   longpoll: false

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/",
    from: :my_app,
    gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

  # Code reloading can be explicitly enabled under the
  # :code_reloader configuration of your endpoint.
  if code_reloading? do
    plug Phoenix.CodeReloader
  end

  plug Plug.RequestId
  plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library()

  plug Plug.MethodOverride
  plug Plug.Head
  plug Plug.Session, @session_options
  plug MyAppWeb.Router
end

router.ex

defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  pipeline :api do
    plug :accepts, ["json"]
    plug :auth
  end

  scope "/api", MyAppWeb do
    pipe_through :api

    get "/token", AccountsController, :token
    get "/accounts", AccountsController, :index
  end

  defp auth(conn, _opts) do
    username = System.fetch_env!("USERNAME")
    password = System.fetch_env!("PASSWORD")
    Plug.BasicAuth.basic_auth(conn, username: username, password: password)
  end
end
3 Likes

Phoenix’s live reload relies on a websocket. The code seems to be missing. Here’s mine:

  if code_reloading? do
    socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
    plug Phoenix.LiveReloader
    plug Phoenix.CodeReloader
    plug Phoenix.Ecto.CheckRepoStatus, otp_app: :sternik
  end
2 Likes

If You use --no-html You won’t have phoenix_live_reload in deps of your mix file :slight_smile:

3 Likes

So since I do not have any HTML and don’t need LiveReload how do I disable or suppress this debug info? I mean if I do not have it, how and why do I get this error or what’s trying to make that request? :sweat_smile: I am just trying to understand.

1 Like

Trying a new project, and have no errors…

Phoenix 1.5.9
OTP 24.0.3

1 Like

Thank you for trying it! Maybe there’s something wrong with my project or configuration.
I’ll also try a new project and see what’s wrong and reply here.

You may also want to double-check that you don’t have any browser tabs that you’ve left open.

3 Likes

Does that mean I can not use a tab to interact with the API? Sorry for my ignorance, but what does it have to do with that? I’m just asking to learn.

You might be getting this error from having a tab open for another phoenix project that did have live reload enabled and its live reload iframe tries to connect to localhost:4000/... which is now used but your api-only project.

4 Likes

Oh that makes sense, and I think that was exactly my case. I just had some other open tabs and after I’ve closed them I do not get that error anymore. Thank you so much for the explanation.

2 Likes