Debugging elixir channels

I need to see all channel events and params received by application and their outputs.
For now I use this construction:

defmodule AppWeb.SomeChannel do
  ...
  # wraps all handle_in
  def handle_in(event, params, socket) do
    Logger.debug("#{event} - #{inspect params}")
    my_handle_in(event, params, socket)
  end

  my_handle_in("some_event", params, socket) do
    ...
  end
  
  my_handle_in("some_event2", params, socket) do
    ...
  end
  # And others
  ...
end

And every channel has this wrapper. I understand how to dry it with macro.
But Is there a some better approach like we can do with plug?

1 Like

Plugs don’t work with channels, so it’s not possible.

If it’s for debugging I would suggest you look into tracing. With tracing you can “listen” to all calls to a function without changing the code in any way. I would recommend the recon package. Using it calling (from iex):

:recon_trace.calls({MyChannel, :handle_in, :_}, 100)

You will be notified about the next 100 calls.

There are packages for printing the calls with elixir syntax recon_ex and tap.

1 Like

Nice, thank you. Do you have experience to connecting remotely to a running phoenix process?

Phoenix 1.3/master has the ability to configure log_join and log_handle_in options so you can get this without the boilerplate :slight_smile:

4 Likes

Finally tried it. It’s that I need in first place, thank you.