Phoenix LiveView socket and en external socket

Hello, I need help with something and excuse me if it seems trivial since I’m still learning phoenix.

I have a simple LiveView to view a position of an object in 3D space, the position comes form an external socket here is how I handle the incoming position

def handle_in("new_msg", %{"x" => posX, "y"=> posY, "z" => posZ}, socket) do
    GameLive.handle_event("new_position", %{"x" => posX, "y" => posY, "z" => posZ}, socket)
    broadcast!(socket, "new_msg", %{x: posX, y: posY, z: posZ})
    {:reply, {:ok, %{response: "Updated"}}, socket}

and here is how I handle the event

def handle_event("new_position", %{"x" => posX, "y" => posY, "z" => posZ}, socket) do
    {:noreply, assign(socket, %{x: posX, y: posY, z: posZ})}

I get this error whenever I try to update the position

[error] GenServer #PID<0.481.0> terminating
** (FunctionClauseError) no function clause matching in Phoenix.LiveView.assign/2
    (phoenix_live_view) lib/phoenix_live_view.ex:1252: Phoenix.LiveView.assign(%Phoenix.Socket{assigns: %{}, channel: GameviewWeb.RoomChannel, channel_pid: #PID<0.481.0>, endpoint: GameviewWeb.Endpoint, handler: GameviewWeb.UserSocket, id: nil, join_ref: "3", joined: true, private: %{log_handle_in: :debug, log_join: :info}, pubsub_server: Gameview.PubSub, ref: "4", serializer: Phoenix.Socket.V2.JSONSerializer, topic: "room:lobby", transport: :websocket, transport_pid: #PID<0.478.0>}, %{x: 5, y: 5, z: 5})

What am I doing wrong?

Thanks in advance.

Phoenix.LiveView.assign/2 only accepts a Phoenix.LiveView.Socket . You are passing in a Phoenix.Socket.

1 Like

I’m not sure about the implementation details but try to send a message to the LiveView instead and call assign/2 in a handle_info/2 when you are handling that message.

1 Like

Can you tell me how to implement this? That is, I have two different sockets, in the first I want to use use Phoenix.Socket, and it works, and in the second I want to use Phoenix.LiveView.Socket, but still uses Phoenix.Socket, that is, how to separate it? Thanks

@Yurii I haven’t looked into the Phoenix LiveView source code recently but I suggest you open a new question where you copy small parts of the code form where the socket comes from because probably the real problem is that you call a function with a socket you shouldn’t call.

Yes, I have already opened it) Thank you