Phoenix.Socket send(pid())

hello,

I have correctly implemented https://hexdocs.pm/phoenix/Phoenix.Socket.Transport.html#module-example

I want to send a message directly to a target socket, without using EndPoint.Broadcast or erlang :PG2
(it’s not a correct way to call getMemberList, then iterate and filter out a single pid for each message sent)

however using a code as (in handle_in callback):

socket_pid = self()
send(socket_pid, “TEST”) #trying to send a message to myself

it throwns an error

please help

What is the error?

Perhaps do you know how to solve this error?

[error] Ranch listener IperiusAppWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.461.0> exit with reason: {:function_clause, [{Phoenix.Endpoint.Cowboy2Handler, :handle_reply, [IperiusAppWeb.Sockets.ChatSocket, {:text, “{“1”:“2”}”}], []},

def handle_in({text, _opts}, state) do

socket_pid = self()

send(socket_pid, {:text, Jason.encode!(%{“1” => “2”})}) # I’m trying to send a test message to myself

[error] Ranch listener IperiusAppWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.463.0> exit with reason: {:function_clause, [{Phoenix.Endpoint.Cowboy2Handler, :handle_reply, [IperiusAppWeb.Sockets.ChatSocket, {:text, “{“1”:“2”}”}], []}, {:cowboy_websocket, :handler_call, 6, [file: ‘/home/ubuntu/Progetti/Iperius/deps/cowboy/src/cowboy_websocket.erl’, line: 528]}, {:cowboy_http, :loop, 1, [file: ‘/home/ubuntu/Progetti/Iperius/deps/cowboy/src/cowboy_http.erl’, line: 254]}, {:proc_lib, :init_p_do_apply, 3, [file: ‘proc_lib.erl’, line: 226]}]}

Seems that in handle_in I cannot send to myself producing a loop, and should use :reply
Now I test sending to a remote socket

General note: ``` will help people understand your posts better

The error message you’re getting is tricky to read because it’s coming from Erlang, but here’s how to break it down:

:cowboy_clear:start_link/4 at #PID<0.463.0> exit 

This means the Cowboy handler got an error and crashed

with reason: {:function_clause, 

The error was that there was no matching function head

[{Phoenix.Endpoint.Cowboy2Handler, :handle_reply, ...}]

This is a Module Function Args tuple, a common idiom in BEAM code. In this case, it means the missing function head was Phoenix.Endpoint.Cowboy2Handler.handle_reply

[IperiusAppWeb.Sockets.ChatSocket, {:text, “{“1”:“2”}”}]

With two arguments: your handler module, and the value passed to send in your example above.

The error appears to be arising from this code, in the Cowboy 2 handler:

which is responsible for passing messages sent to the process into your module.

Your handle_in function returns the value of the last expression in it, the send call. But Phoenix.Endpoint.Cowboy2Handler.handle_reply only supports a fixed set of reply shapes:

as documented in the typespec for Phoenix.Socket.Transport.handle_in/2

2 Likes

Thanks for your answer

The error is gone changing the send() argument, I correctly insert the target pid, the text, but the target websocket receive nothing

Channel.ex server.ex

def push(socket, event, message) do

%{transport_pid: transport_pid, topic: topic} = assert_joined!(socket)

Server.push(transport_pid, topic, event, message, socket.serializer)

end

@doc “”"

Pushes a message with the given topic, event and payload

to the given process.

“”"

def push(pid, topic, event, payload, serializer)

when is_binary(topic) and is_binary(event) and is_map(payload) do

message = %Message{topic: topic, event: event, payload: payload}

send(pid, serializer.encode!(message))

:ok

end

seems at the end using send(pid…

:-???

Please post the updated code; it’s difficult to say what might be causing the described behavior without it.

Solved reinstalling elixir erlang from stable releases