Do I even need Router, Controllers and Views if I don't plan on developing HTML through Phoenix?

I’ve been continuing to read and study up on the Phoenix docs. If I understand this correctly, it sounds like Router, Controllers and Views are meant for those who are developing a complete, dynamic website entirely within Phoenix itself.

Right now, I already have a large, existing PHP/MySQL/HTML site developed and served through Apache. I have a Node.js powered chat room built, which manipulates existing client HTML/JS already served from Apache. My plan is to replace Node.js with Phoenix and Elixir. Basically what I’ll be doing is capturing Phoenix commands/broadcasts sent to the client side to manipulate HTML already loaded up on the client side, using JS. I won’t be developing any HTML templates or anything within the Phoenix app, since the layout is already loaded up for the client via PHP/Apache.

So in my case, does it make sense to simply remove Router, Controllers and View from my app, and build my authentication and chat features using just Endpoint, Plugs, Channels and JS?

If so, what things do I need to consider or think about in removing these? And does it increase Phoenix performance by removing these? Or is my thinking wrong and there is actually a reason to keep these, even if I’m not developing HTML within Phoenix?

Thanks

What will you be using to send the commands to your client? If you are not using Channels, what is your reasoning for using Phoenix?

As I was explaining above, I will still be using Channels, as it seems this is the backbone for the server sending information back to the client. Although, it doesn’t sound like I need router, controllers or views though (unless I’m wrong?), as I won’t be developing HTML pages inside Phoenix, it’s already set up on PHP/Apache. I’ll be using client-side JS to process the signals coming from Phoenix channels.

Ah, sorry, I missed the Channels part! I also made a project using only Channels and I can tell you that I don’t have any views or controllers. I still have the router.ex file but I’m pretty sure I can just remove it since it’s not really doing anything. If I were you I’d leave it there with no routes specified, there shouldn’t be any real performance benefit.

1 Like

Not really no

At a minimum your application will probably need to support health checks and while you can do that in raw plug, if you have a Phoenix endpoint up and running you may as well use a phoenix router for them. The router will also come in handy if you want to load in something like LiveDashboard or other diagnostics pages.

5 Likes

Cool. Thanks for the insight. I’m starting to become more and more comfortable understanding the basics of the whole Phoenix setup.

Just a side question:

Do you have any user auth set up on your project? I had installed phx.gen.auth, but I’m now realizing that this installs a bunch of html files set up with router, controller and views, which I don’t think I’ll be needing.

My auth strategy now (for the first step) is to create a plug and use a function like conn.req_cookie[‘userid’] and conn.req_cookie[‘token’] to retrieve two cookies that PHP puts in the user’s browser after login: one for the userid and one for a token that was generated in php and stored in the mysql database. Then inside the phoenix plug, run MySQL query to verify the user/token combination. Think I’m on the right track?

The router will also come in handy if you want to load in something like LiveDashboard or other diagnostics pages.

Great point. I do like having LiveDashboard available. I’ll probably leave the default Router and keep it as-is.

Side question: Do you know of an admin panel like LiveDashboard where I can view a list of all of the users on the site, listing their usernames, other info etc? Or have options to initiate chat with them, kick them out of rooms, delete sockets, ban, etc?

1 Like

If You use --no-html with phx.new, You won’t have views and templates.

If You comment the last line in your endpoint, You won’t use the router… but I never tried :slight_smile:

Authentication is different with an API and You probably need to roll your own, and not use phx.gen.auth.

2 Likes

For the side question, You can use Phoenix Tracker to see who is where…

There is Presence, but what You want is to track people.

2 Likes

phx.gen.auth concerns itself with the authentication part of your app, you can pair it with something like Guardian to handle the authorization for the APIs.

But since @peppy already handles the auth in the PHP app, yeah, phx.gen.auth is unnecessary.

Don’t know how your app is structured, but you can have problems requesting cookies if they are in different domains (or even different subdomains if the cookie is a secure one IIRC).

Without knowing more about your app it is hard to know the best solution, my first naive approach would be that as soon as you log in your php app calls an endpoint in the phoenix app that will generate a Bearer Token that the php app will send to the client, the client will send this token when establishing the connection with the phoenix app, if the Token is valid you create the websocket, if not, just ignore.

2 Likes

I haven’t used it personally but this seems quite full-featured. aesmail/kaffy: Powerfully simple admin package for phoenix applications (github.com)

1 Like