Hii I am new to elixir, i want to receive a message on remote client, send from phoenix server process on a channel. I am able to connect to socket and push a message from remote client to phoenix server but am not able to receive a message.
I wish to signal a process running on remote system when a button on Phoenix Live View is pressed using socket.
Remote Clients are two separate RPI’s running elixir process.
Phoenix Server is running on a laptop, act as a dashboard for RPI’s.
All three are connected through internet.
Connecting to Client using PhoenixClient Library in Elixir, which connects to a socket created in Phoenix Server, and joins a channel and can push data to phoenix server.
What i want is to get, data entered by user on LiveView to Remote Client.
When server is pushes a message with event name say “incoming:msg” - you can listen for it by implementing handle_info with pattern match on Message struct.
defmodule MyApp.Worker do
use GenServer
alias PhoenixClient.{Socket, Channel, Message}
# start_link ...
def init(_opts) do
{:ok, _response, channel} = Channel.join(Socket, "room:lobby")
{:ok, %{
channel: channel
}}
end
# do some work, call `Channel.push` ...
# below function is what is handling the server push
def handle_info(%Message{event: "incoming:msg", payload: payload}, state) do
IO.puts "Incoming Message: #{inspect payload}"
{:noreply, state}
end
end
You will have to implement similar handle_info with your message with event name “do_some_work_push”:
def handle_info((%Message{event: "do_some_work_push", payload: payload}, state) do
# do your processing here
{:noreply, state}
end
In phoenix_client library (refer to line 238), func transport_receive/2 is sending a message to channel process using send, hence if you are implementing a channel using GenServer you should pattern match on event attribute of Message param in handle_info/2. handle_info/2 func can contain logic like signal a process, etc.