I have a basic phoenix hello-world channel that is working as I expect. It looks like this:
defmodule MyApp.Web.HelloChannel do
use MyApp.Web, :channel
def join("hello:"<>world_id, _params, socket) do
# :timer.send_interval(15_000, :hi)
{ :ok, assign(socket, :world_id, String.to_integer(world_id)) }
end
def handle_info(:hi, socket) do
IO.puts '^^ handl_info ^^'
count = socket.assigns[:count] || 1
push socket, "hi", %{count: count}
{:noreply, assign(socket, :count, count + 1)}
end
end
I also have another module which opens a TCP connection to a VowpalWabbit server. It looks like this:
defmodule MyApp.VowpalWabbit do
use GenServer
def format_client_data(client_data) do
String.trim(client_data)
client_data = client_data <> "\n"
end
def predict_inflation(pid, client_data) do
client_data = format_client_data(client_data)
GenServer.call(pid, {:predict_inflation, client_data})
end
@initial_state %{socket: nil}
def start_link do
{:ok, pid} = GenServer.start_link(__MODULE__, @initial_state)
end
def init(state) do
opts = [:binary, active: false, packet: :line]
{:ok, socket} = :gen_tcp.connect({10, 247, 4, 104}, 26542, opts)
{:ok, %{state | socket: socket}}
end
def handle_call({:predict_inflation, client_data}, _from, %{socket: socket} = state) do
:ok = :gen_tcp.send(socket, client_data)
{:ok, msg} = :gen_tcp.recv(socket, 0)
{:reply, msg, state}
end
end
Each of these works independently, but now I want to hook them up. When I receive a message on the channel, I would like to pass it along to the VowpalWabbit module so it can get the result from the VowpalWabbit server, and then I want to pass that result back down the client on the channel.
How can I accomplish this?
Thanks.
P.S. Inside the project’s lib/
folder, my directory structure is as follows:
└── my_app
├── application.ex
├── vowpal_wabbit.ex
└── web
├── channels
│ ├── hello_channel.ex
│ └── user_socket.ex
├── controllers
│ ├── config_controller.ex
│ └── page_controller.ex
├── endpoint.ex
├── gettext.ex
├── router.ex
├── templates
│ ├── layout
│ └── page
├── views
│ ├── error_helpers.ex
│ ├── error_view.ex
│ ├── layout_view.ex
│ └── page_view.ex
└── web.ex