madshargreave

madshargreave

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?

Most Liked

ibarch

ibarch

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

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement