sabri
March 28, 2017, 3:20pm
1
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
sabri
March 29, 2017, 7:38am
4
1 Like
NobbZ
March 29, 2017, 7:48am
5
But it has to have, please see documentation of the callback:
https://hexdocs.pm/phoenix/Phoenix.Socket.html#c:id/1
1 Like
sabri
March 29, 2017, 8:21am
6
Thanks it worked , I added:
# Returning `nil` makes this socket anonymous.
def id(_socket), do: nil
but what is meant by anonymous socket?
1 Like
NobbZ
March 29, 2017, 8:27am
7
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
sabri
March 29, 2017, 8:31am
8
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
NobbZ
March 29, 2017, 9:15am
9
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