How to recieve Phoenix Remote Client message

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.

Can you share more details about what you are trying to do:

  • What is remote client - elixir, app, browser ?
  • How are you connecting to client? phoenix.js or any other
  • remote system is server or something else ?

[Post Deleted by user]

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.

docs show below example:

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
2 Likes

Thanks, I will try it out.
Just before this code the doc says:

Channels are usually constructed in a process such as a GenServer. Here is an example of how this is typically used.

Isnt handle_info only available for channel definition (in room_channel.ex file in Phoenix Directory)? Can it also be used by RPI Client?

Two different projects:

  • Phoenix Server contains socket, channel to which RPi clients connect.
  • RPi Client contains Phoenix Client which has socket and channel. I am assuming this as plain Elixir project.

MyApp.Worker is a client GenServer which represents channel on RPi. The documentation is giving example of creating channel using GenServer.

handle_* callbacks are defined on GenServer.

GenServer doc has a section Receiving “regular” messages which explains about this.

Besides the synchronous and asynchronous communication provided by call/3and cast/2, “regular” messages sent by functions such as Kernel.send/2,Process.send_after/4 and similar, can be handled inside the handle_info/2callback.

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.

3 Likes

@kartheek Thank you so much for the elaborated answer, it worked and I now have pretty good understanding of these concepts.

2 Likes