** (UndefinedFunctionError) function MyApp.Schools.SchoolSocket.id/1 is undefined or private

I have two EndPoint but I have this error:

** (exit) an exception was raised:
    ** (UndefinedFunctionError) function MyApp.Schools.SchoolSocket.id/1 is undefined or private
        (MyApp) MyApp.Schools.SchoolSocket.id(%Phoenix.Socket{assigns: %{bus_id: 3}, channel: nil, channel_pid: nil, endpoint: MyApp.Schools.Endpoint, handler: MyApp.Schools.SchoolSocket, id: nil, joined: false, pubsub_server: MyApp.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: nil, transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.638.0>})
        (phoenix) lib/phoenix/socket/transport.ex:169: Phoenix.Socket.Transport.connect_vsn/6
        (phoenix) lib/phoenix/transports/websocket.ex:73: Phoenix.Transports.WebSocket.init/2
        (phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:12: Phoenix.Endpoint.CowboyWebSocket.init/3
        (cowboy) /Users/samir/Documents/projects/elixir/deps/cowboy/src/cowboy_handler.erl:64: :cowboy_handler.handler_init/4
        (cowboy) /Users/samir/Documents/projects/elixir/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

This is my endpoint:

defmodule MyApp.Schools.Endpoint do
use Phoenix.Endpoint, otp_app: :MyApp

socket “/socket”, MyApp.Schools.SchoolSocket

MyApp.Schools.SchoolSocket in folder:

web/channels/schools/school_socket.ex

Any idea? why I get function MyApp.Schools.SchoolSocket.id/1 is not found?

1 Like

Can you paste the contents of your school_socket.ex file? Does it have an id function expecting one argument defined on it?

1 Like

Maybe you are using id in wrong context and there ano variable like id, so compiler thinks it’s a function, but there’s no function id as well.

1 Like

@josevalim @PatNowak
Kindly see my school_socket.ex file here: https://gist.github.com/hopewise/aec9ed8b8fe4ced2f818091e4940ff8e
it has not id function

1 Like

But it has to have, please see documentation of the callback:

https://hexdocs.pm/phoenix/Phoenix.Socket.html#c:id/1

1 Like

Thanks it worked :slight_smile: , I added:

# Returning `nil` makes this socket anonymous.
  def id(_socket), do: nil

but what is meant by anonymous socket?

1 Like

Well, it means it has no name, so you can’t broadcast to it as in MyApp.Endpoint.broadcast("users_socket:" <> user.id, "disconnect", %{}). At least thats what I read from the docs, but I haven’t used channels so far.

1 Like

Here is an example that is working with nil socket id:

def join("buses:" <> bus_id, _params, socket) do
    {:ok, socket}
end

def handle_in("new:bus_location", msg, socket) do
    broadcast! socket, "new:bus_location", %{bus_id: msg["bus_id"], long: msg["long"], long: msg["lat"]}
end

it will broadcast to all subscribers of topic "buses:" <> bus_id

1 Like

Well, you get the socket passed in in your example, while you construct from its name it in mine.

It’s a bit like calling foo.() vs (fn () -> :a end).().

2 Likes