Must I use Phoenix framework?

Since you know what I want, I strongly believe that those books recommended will put me on the right path. I will spend time digesting them and when I am done, I believe I won’t be too naive. I love this community!
Any book that covers UDP with Phoenix too would be great.
Thanks for your contributions

1 Like

You’re very welcome! Please feel free to post any questions or thoughts you might have to the community and don’t worry about being “too naive” and such things—the vast majority of us would be happy to help. We’re all learners with varying degrees of experience :slight_smile:

3 Likes

FWIW I’ve had good luck implementing APIs with Cowboy or Plug in commercial projects. The consumers of the APIs were either single page apps (React, Angular) or third-party backends. In those cases we didn’t need all the extra features that come along with Phoenix so we opted for the lighter approach.

Since you’re new to Elixir, I recommend picking 2 or 3 libraries/frameworks to evaluate a small part of your app in and getting hands-on experience with each one. Not only will that give you a better idea of each library, you’ll gain more experience with the Elixir language and ecosystem as well.

If you need to interface with exchanges via UDP or provide a UDP connection to your clients, check out the documentation for gen_udp.

2 Likes

The new keynote from Elixirconf 2018 might give you another reason on why to use Phoenix.

1 Like

Curious, is there a phoenix UDP story. is there even a cowboy UDP story?

1 Like

I have no idea Sir. Just getting my feet wet with my new found passion.

No. They’re completely not suited to dealing with connectionless datagrams.

1 Like

https://learnyousomeerlang.com/buckets-of-sockets

1 Like

No UDP with phoenix, yes UDP with cowboy. Cowboy is pluggable and it supports theoretically any transportation formats, like HTTP via TCP is via ranch as I recall (or was that the other way around…). ^.^

1 Like

Sir, is there a tutorial/doc for UDP with cowboy? Thanks

Please, do you know any tutorial on implementing APIs with Cowboy or Plug? Thanks

Please, do you know any tutorial on implementing APIs with Cowboy or Plug?

I recommend taking a look at the readme for Plug. The Plug.Router section shows how to create route handlers for your API – it’s incredibly simple. If you have an application large enough to need to keep the business logic out of the routes (basically any non-trivial app), just create a models directory, put that logic in there, and use the route handlers similar to how you would use a controller in an MVC framework: control access, validate parameters, make calls to models.

One trick I like to do that’s not in the Plug docs is to create helpers for the HTTP responses:

defmodule Routes.Helpers do
  import Plug.Conn

  def ok!(conn, body) do
    conn
    |> send_resp(200, body)
    |> halt
  end

  def not_found!(conn) do
    conn |> error!(404, "Not Found")
  end
end

That way you can respond to a route using code that’s a little easier to read in my opinion. It’s also a good place to handle JSON encoding if your API produces JSON responses (not shown here).

get "/hello" do
  conn |> ok!("world")
end

match _ do
  conn |> not_found!
end

That’s just my personal preference though. Do what makes sense for your project.

Regarding Cowboy, there’s a pretty extensive Cowboy User Guide. Just keep in mind that Cowboy is implemented in Erlang, so you’ll have to do some mental conversion from Erlang to Elixir when interfacing with it. It’s not too difficult, but you may want to be comfortable with the basics of Elixir first. Plug is probably going to be the easier of the two to get started with. Plug uses Cowboy under the hood in its typical configuration (and Phoenix uses Plug). An advantage to building directly on Cowboy is if you’re concerned about keeping your dependencies to a bare minimum (security requirements, maintainability, simplicity, etc.).

2 Likes

Well if you don’t need the extreme performance that ranch is built for then just using gen_udp that comes with the beam is perfectly fine, a good tutorial is at: Buckets of Sockets | Learn You Some Erlang for Great Good!

As for ranch, I’m unsure if a udp version of it is built currently, I could have sworn there was at one point years ago but I can’t recall the name of it if so…

Thanks a lot @amaclain

I would revisit whether you even want to use UDP - there are plenty of scenarios where TCP is much faster than UDP and generally it’s the sane choice for building an API if you’re unsure about which one will fit your needs.

4 Likes

@dgmcguire this is informative. Thank you Sir