1

I need to call a function inside the controller from phoenix channel. This is my phoenix channel

    //dashboardChannel.ex
    def join("dashboard:lobby", payload, socket) do
        IO.puts "Entered Room"

        if authorized?(payload) do
          {:ok, socket}
        else
          {:error, %{reason: "unauthorized"}}
        end
    end

This is my handle_in function

    //dashboardChannel.ex
    def handle_in("new_msg", %{"uid" => uid, "body" => body}, socket) do

        broadcast_from! socket, "new_msg", %{uid: uid, body: body}
        MyApp.Endpoint.broadcast_from! self(), "dashboard:lobby", "new_msg", %{uid: uid, body: body}

        {:noreply, socket}
    end

The below is my controller function, The route for this controller will be "/users"

    //dashboardController.ex
    def index(conn, _params) do
        IO.puts "Enters Users Controller"
        MyApp.Endpoint.broadcast_from! self(), "dashboard:lobby", "new_msg", %{"uid" => "ac4452s", "body" => "Sample User"}
        IO.puts "Must have executed User Controller"

        users = Accounts.list_users() // It will list all my users
        render(conn, "index.json", users: users)
    end

I'am new to phoenix and elixir. I need to call the "/users" controller function from the phoenix channel above. How do i call call that. Is there any way to call a controller function from phoenix channel and also call the handle_in function which is "new_msg" from a controller method. Thanks in advance

The below is my requirement

  1. I need to call function of dashboardController's which is my "index"(route "/users") from dashboardChannel's handle_in function of new_msg in topic "dashboard:lobby"
  2. And also i need to call the handle_in function of dashboardChannel's new_msg in topic "dashboard:lobby" from dashboardController's index
blackgreen
  • 34,072
  • 23
  • 111
  • 129
Jeeva
  • 1,550
  • 12
  • 15
  • There are no _methods_ in Elixir, those are all _functions_ and being functions they might be called from everywhere. Calling `UsersConroller.index/2` from the channel makes absolutely no sense since it ends up _rendering_ stuff which is obviously not applicable in the channel context. You’d better explain what are you trying to achieve and then we could be of any help. – Aleksei Matiushkin Jun 05 '18 at 10:10
  • @mudasobwa, Whenever a record gets inserted, i need to broadcast it, So I need to access the handle_in function of a specific topic. Similarly whenever a record sent through channel from client, i need to inserted it by accessing the controller function from channel – Jeeva Jun 05 '18 at 11:03
  • I found this https://hexdocs.pm/phoenix/Phoenix.Endpoint.html#content and also broadcasting to an external topic https://hexdocs.pm/phoenix/Phoenix.Channel.html But after following the steps, none gets reflected – Jeeva Jun 05 '18 at 11:06
  • This seems to me like you don't really want to call the full controller function but rather some parts of it. In that case you should refactor the relevant logic into a separate module and call that from the controller and the channel. – Sascha Wolf Jun 06 '18 at 09:56
  • @Sascha Wolf, correct. But i need to broadcast it to my channel, is what the problem here i'm facing. Everytime when a record gets inserted, i need to broadcast it whether it may be controller or channel. In channel i can able to do that, but in controller function i can able to do that. – Jeeva Jun 07 '18 at 05:47

0 Answers0