rawkode

rawkode

Channels & Controllers

Hi,

I have started to expose some of my HTTP endpoints to a web socket channel, to provide mobile apps with a single stream to access the API.

I’ve found that there’s a lot of duplication between the two and I’m trying to work out the correct way to handle this in Elixir, with Phoenix.

In my previous, non-functional, life; I’d have had the transport specific endpoint handle the creation of a Command object and pass to a CommandBus.

Is there a functional route for this? I’m weary I’ll follow my old path into anti-patterns if I don’t ask.

NB: Has anyone else managed to expose endpoints over HTTP and WS without replicating code? Could this be done with a Plug?

Thanks.
David

Most Liked

PatNowak

PatNowak

Maybe attach some snippets of code to be understood better.
Why you want to expose HTTP endpoints to channel? Endpoints are for request/response cycle and channels are for stateful conversation. IMO You shouldn’t mix two of these.

rawkode

rawkode

I understand that web-sockets aren’t generally used for this, but there’s no reason they can’t adopt a CORRELATION_ID method, like AMQP, to handle request / response. As mobile apps rely on web-socket connections more, why would they open new connections? For further reference, http://wamp-proto.org looks cool.

I can create a struct

%RegisterUser{
    forename: "David"
}

Channel

  def handle_in("/user/create", command = %{ "identifier" => identifier, "email" => email, "metadata" => metadata}, socket) do
    with {:ok, server_identifier} <- CreateUserFunction(identifier, email, metadata)
    do
        broadcast! socket, "UserCreated", %{ "egress" => server_identifier, "ingress" => identifier}
    else
         {:noreply, socket}
    end
  end

Controller

  def create(connection, _params = %{ "email" => email, "identifier" => identifier }) do
    with {:ok, server_identifier} <- CreateUserFunction(identifier, email, metadata)
    do
      connection
        |> put_status(200)
        |> json(%{ "egress" => server_dentifier, "ingress" => identifier})
    else
      _ -> connection
          |> put_status(400)
          |> json(%{})
    end
  end

and send that to a function, from both the controller and channel functions. What I want to avoid is one being updated independently of the other. Is there a functional pattern for this?

Thanks

l00ker

l00ker

Hi,

I’m looking to do something similar but being an Elixir/Phoenix newbie (like < 2 weeks in) I’m still reading books and still feeling my way around things. I’m coming at this from starting to build APIs with FeathersJS where the real-time aspect using web-sockets is a core part of the process. I want to use Phoenix channels for the API in the same way FeathersJS does. Simple enough… and (currently) my thoughts are the same as yours. Trying not to duplicate code in the channels and controllers.

Til I get a better handle on Elixir/Phoenix I’m not too much help ATM but thought I’d chime in. But I’m glad to see someone else is on the same path. :slight_smile:

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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

We're in Beta

About us Mission Statement