revid

revid

Best way to use Phoenix & Next.js?

Hello!

I have been searching the web for some resources on this topic but have fallen short. I am in the process of creating a web and mobile application that will utilize Phoenix Channels for realtime communication. After some prototyping, I have decided that rather than use LiveView, I will use Phoenix Channels to handle realtime connections and build out the application in React.

In the best case scenario, I’d like to use something like Next.js or Remix for my web application. The added benefit for me is that it can handle both static and dynamic web pages.

My thinking was that I could use Next.js as both a CRUD API for various actions on the site and use it as a reverse proxy to Phoenix for any realtime communication.

My concern is that doing this will take away some benefits of talking to Phoenix directly as I would have to do an additional network hop. Also, I’m not sure that Next.js supports proxying websocket requests, so that might not even be an option.

Does anyone know what the right approach here would be? I found this article that sort of summarizes my approach but I don’t know if there are other ways of going about this.

Most Liked

hellosmithy

hellosmithy

I’ll caveat that I’m very new to Elixir and Phoenix so take my answer with that in mind. However I’ve been coding in React/Node for many years.
Without knowing the exact requirements of your app, this sounds to me like unnecessary overhead just to get some pages statically generated. Why not start with just Phoenix across all your routes and look to optimise with static generation later if you have heavy traffic?
It sounds like you’d be trading all the ease of development that Phoenix offers to benefit some of your pages load times / server usage but you’d be adding layers of complexity and possibly latency everywhere else.

tfwright

tfwright

It sounds like you’re not sold on LV, which is completely understandable, but why not use React for your FE with a Phoenix API+channels, which is not an uncommon setup (this is my stack at work for the last 3 years)? I don’t see the reason for introducing a third layer just for a CRUD API? Phoenix can serve static pages just fine as well…

revid

revid

I’m in the same boat! And I ultimately agree with your advice. It’s definitely unnecessary overhead, but I also think this is a pattern that could be quite common, especially for people that want to dabble in Phoenix, without being 100% sold on LiveView or are building applications outside of just a web app.

That said, I am doing this as a learning experience/experiment so I’m alright with the overhead for now. I found a few things that touched on what I was going for since posting this.

I’m not all in on Next.js /the JavaScript framework itself. I actually wanted to give Remix a fair shot, and reading through this doc gave me some ideas on how I might be able to approach the separation of server-client state: Backend For Frontend | Remix. Also, I found this repo on another thread that I might borrow some ideas from: GitHub - arcanemachine/phoenix-todo-list: Yet another todo list. Made using the Phoenix web framework. Features: Basic CRUD, LiveView CRUD, REST API, OpenAPI spec, Playwright E2E tests, GitHub Actions CI, and more! · GitHub.

There are many downsides to doing what I’m suggesting if I plan on simply creating a website. This approach would mean that for any pages that hit the Node server first, I am mostly likely going to be doing unnecessary network hops. Client → Node server → Elixir → DB → Node → Client. However, for Phoenix channels, I am going to talk directly to my Elixir service and not overcomplicate that.

In addition, Auth and any communication to third party services will also happen in my Phoenix application. So really my Remix setup will be making redundant calls and ultimately provide no other value than an opinionated framework for writing JS/React.

The biggest hurdle so far for me has actually been finding an out-of-the-box auth solution. phx.gen.auth requires HTML (at least for the code generation) and while I have been trying to repurpose that for an API architecture, it is quite challenging for a newbie.

Either way, I’m going to continue with this approach, maybe it will make sense to use the SPA mode that Remix just introduced, but either way I’m not quite ready to give up on JS… Probably controversial, but the Phoenix development experience compared to the React toolkit isn’t there yet and I’m not ready to give it up.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement