Send data from one function to other

defmodule TestZeeshanWeb.PageController do
  use TestZeeshanWeb, :controller
  use GenServer
  
  def start_link(opts \\ []) do
      GenServer.start_link(__MODULE__, :ok, opts)
  end

  def init (:ok) do
      {:ok, _socket} = :gen_udp.open(6789)
  end

  # Handle UDP data
  def handle_info({:udp, socket, ip, _port, data}, state) do
      IO.puts inspect(data)
      
      {:noreply, state}
  end

  # Ignore everything else
  def handle_info({:udp, socket}, state) do
      {:noreply, state}
  end

  def index(conn, _params) do
    render(conn, "index.html")
  end
end

I want to send data from handle_info to index didn’t got data.

Big Picture of project is to get data via UDP Port and show it on the Web View. above code is working fine and getting data from UDP port. IO.puts inspect(data) also working and showing that data on my terminal. But I want to use this code in Phoenix.

I am total new in web programming and have not any clear idea, how to show handle_info data on Web View.

I’m currently not understanding what exactly your problem is.
Do you want to access the state that you have stored with your Genserver?

If so you could simply implement sth like this:

  def handle_call(:get_state, _from, state) do
    {:reply, state}
  end

I want to send the data from handle_call to another process/Controller.

Hi @aqeel-ahmad-plc,

If you want to send data from a genserver, use the handle_call and send a reply back, or invoke the process / controller function inside your handle_call operation. But, it is not very clear, as to what is the thing that you are trying to achieve. Are you trying to build some kind of chat app?

Thanks.

No. This is somthing like hardware project. I am total new in Phoenix. so big picture is, I have a hardware that send me data on UDP port. I want to read that data from UDP port and show data on website. Data came from UDP port is in the form of json.
I want to done this all in Phoenix.

Hi @aqeel-ahmad-plc,

Then, you have to move your genserver to a seperate module and start it along with phoenix application. When it gets the data, use the handle_info to persist your data somewhere or call the function where you want your data inside your handle.

Thanks.

1 Like

I got your point. But please describe the method also. I am really a new person in Phoenix thing.

Hi @aqeel-ahmad-plc,

You haven’t still told us about your data persistence, are you planning to store it somewhere or just pass it along.

I want to store on database and show on screen also.

I guess this is what you are looking for:

def handle_info(pattern, _from, state) do
    ##########HERE GOES YOUR DB FUNCTION#############
    {:noreply, state}
end
Genserver should be moved to a different module

In your controller.ex:

def index(conn, _params) do
########FETCH FROM DB##########
render(conn, "index.html") ###This can be a json response also
end

This is just present in any phoenix example application, let me figure out some examples for you!

1 Like

Thanks. Your rply give me idea to where I put function to store data in database.

But Actually I want to show data in real-time. Data stored in database is just few. Like I got data 3 times in a second. and store once in a day. but want to monitor that data full day on screen in form of graphs. FOr now just want to display readings on screen,

1 Like

Hi @aqeel-ahmad-plc,

I understand, but it’s always good to persist your data somewhere, it may be a simple cache like ets ( you might want to learn this) and reading from the cache will be way faster than reading from DB.

The example can educate you on ETS based cache!

This can educate you on Phoenix.

Note: The actual phoenix docs are excellent, if you are comfortable please go through them as well

The live component of your application could be done with drab (https://github.com/grych/drab).
Or you wait until phoenix live-view (https://dockyard.com/blog/2018/12/12/phoenix-liveview-interactive-real-time-apps-no-need-to-write-javascript) gets released.

1 Like