Action_fallback for channels?

Hi

I love the new action_fallback macro in my controllers. My controllers are now very clean and look something like this

def create(conn, params, identity, _claims) do
    with {:ok, message} <- Messenger.send_message(identity, conn.assigns.conversation, params) do
      conn
      |> put_status(:created)
      |> render(Shared.V1.MessageView, "show.json", current_identity: identity, message: message)
    end
  end

Now, I want to expose the same web API, but over websockets as well. I am thinking something like:

def handle_in("request", %{"method" => "messages.create", "data" => data, "context" => %{"id" => conversation_id}} = payload, socket) do
    with {:ok, message} <- Messenger.send_message(socket.assigns.current_identity, conversation, data) do
      payload = Shared.V1.MessageView.render("show.json", current_identity: socket.assigns.current_identity, message: message)
      push socket, "response", payload
    end
  end

Ideally, similar to how it works with action_fallback in the controller, changeset errors would bubble up and be handled somewhere else.

Any suggestions on how best to accomplish this?

7 Likes

@chrismccord, @josevalim could you please help with this question?

1 Like