YouTubevideo· Jan 15, 2025· 35:52
I think it's about time I give this an honest dry Phoenix the framework has always been appealing to me mostly just because of how similar in maybe mindset and developer experience it seems to be to something like larl and rails and live view which is the kind of websocket connection is very was the inspiration to Livewire I believe Chris mcord kind of published about it or posted about it
on Twitter way back when and Caleb porio thought hey this would be something really cool that we could build within Live Wire within larel as well and of course they're not Apples to Apples they're kind of like apples to oranges in a little way cuz live view at least to my knowledge is websockets where live wire is HTML over the wire but I wanted to actually take some time and dive into Phoenix dive into Elixir and see if I
can figure it out what I like what I don't like and this is my perspective as a PHP Lille developer first look at Phoenix the framework and live view now I have read like some Elixir stuff I have like looked at specific tutorials and just like things but I I'm not familiar with this kind of functional syntax or whatever you want to call this I actually don't know too much about what you would call this stuff here um I
understand what it does it looks like you know this would be a module is probably what a PHP class is akin to I I would assume um you're using the Phoenix live view this is probably saying okay I want this timeline live to actually use everything that's available to me within live view and then I don't know what this is I'm guessing this is just HTML so inline HTML similar to like what you could do within Live Wire we have this
inline HTML and then you're basically saying when I'm rendering this um I want to mount a socket subscribe to Twitter and this would probably be the whole thing that you would need then to yeah see all of this so someone was to create a new post like that it just automatically comes in because I believe this is all websockets it's just saying I want to mount this socket I don't know what that what this
does stream insert but it looks like it it's it's web sockets without that much configuration um how I would build this or how I would think about building this is really interesting let's see if I can get something up and running get started so we have Phoenix Express single command get you up and running really cool I'm wondering I actually haven't seen like this new website I had I I have technically looked at Phoenix and live view but just basically followed
instructions I haven't really given any thought and how I put in thought is talking through it so like this video um so yeah let's go ahead I believe I have mix and stuff all up and running I'm just going to copy this just in case I don't but let's see so if I open up my terminal and let's go ahead and just paste that in yep Elixir is already installed does this create a new app my
app this might create a new app I might not want this okay well I got it up and running which is that's awesome it's awesome to be able to have one you know command like that and be up and running I don't I didn't necessarily want it I thought I had maybe like I'm guessing it installed a bunch of stuff okay my app that's where it installed in so I'm just going to
remove my app and maybe in my code I'm going to run that script again and okay it looks like I could just probably add this on so maybe we'll just do like um like curl and then we'll say this is uh you know Phoenix start okay we're still up and running so you can finish your Elixir setup Elixir has been installed activating current shell um okay turn sure it's always
active I don't know if I have that but maybe I can check so we have uh Vim CSR HRC is that not it aha there we go and I just want to check that the Phoenix is there and you can't make fun of me for um how slow I
am in Vim I know that I can do a bunch of stuff to like make it's a lot better um but this is all I can do right now okay so we're just going to say this we're paste that in there um and I'm going to save that we're going to source that and then we should be good to go so great this is our Elixir app so I assume
now that I have that I could say like elixir DV yeah and mix- V like mix works okay CU mix I believe is similar to like what HP Artisan would be just from the little I know so let's go ahead and see what this uh via mix. new okay still start application using mix phoenix. new okay so that's how you would create a new application that's kind of like the Lal new installer so Phoenix Express I I
really like this single command um it's nice so automatically pick one of post creser my SQL and fall back to SQL light I'm curious which one it fall back or fell back to I should say so if we have cd Phenix start where did this install to oh maybe it installed it as a un underscore so CD Phoenix yeah okay interesting so open up code so we have
this mix. EXs assets so this is where our CSS JavaScript and this is tail and config okay everything's just kind of configured out of the box which is super nice we have our config I would imagine here's where I can see like what the database it actually chose would be lib so we have Phoenix start I believe most of our application files going to live in this lib folder I could be wrong but
just from the little I've scene yeah so we have the Phoenix start we have a repo mailer application and then we have controllers here so this is the this is the Phoenix star web I don't know where the actual like maybe it's assets like the vendor like where is all of this actually living or or this I it's actually living so we have a router and let's see if I can just get like another
page up and running if if I'm starting so Phoenix start web it's going to the page controller home okay very nice and so it's rendering this connection home but I don't know where this home is coming from maybe from components app rout so app yeah this is like the the layout itself page T there we go this is the this is the actual page so in pages
so if I was to create like a new I was to say like about. html. heeks I don't know how you would say that hex like that's what I want to say so anything that is filled in here flash group interesting so imagine like if I was just to say um like an H1 of about then in uh maybe we'll just say in
our routes so route. ex where was the what was that router yeah router okay of scope of Slash we want to pipe through the browser get the Slash and send it to the page controller of home I'm curious if you would add like a different controller I wonder if Phoenix has like some API like a CLI for controllers type stuff but I would imagine maybe just in the in the page
controller what I would just then Define is like about so connections yeah and then we could render the about page and maybe I'll I'll include the layout for this one to see what that layouts that it gave us is like that app wonder if I could say a specific layout like maybe layout app does this use root like I
don't know the difference between that but that makes sense like we have page start and then in uh router I want to then and scope the slab PPE through browser get slash about okay I didn't break anything but if I go to slab no route found forg
slab okay what did I not do so we were scoping this or pring it through the browser maybe I don't have another scope um yeah other Scopes may use custom Stacks okay now I just want to get BCE and pushed it to the page controller bout yeah I'm doing it I I I just I wrote I wrote Phoenix I wrote Elixir I
understand um one of the things that I I think Phoenix allows you to use um to set up off really simply like if I was to say mix help what options do we have here we can generate live view templates I'm just going to zoom out a little bit so I can see everything you're not going to be able to see it but I'm going to be able to see it a little better okay mix phoenix. gen. off okay so generates
authentication Logic for a resource and then gen. live so it's phoenix. gen looks like so like mix phoenix. gen. off okay we need to describe a context module name followed by the scheming module and it's plural name so accounts I wonder what the difference between like accounts and user is accounts. user and payments. user very
interesting so you kind of have these different resources in that sense um live view based authentication system I want to create it perfect so we have please refetch your dependencies so we can mix dip.it okay and then what else were we supposed to do uh mix Ecto do migrate so that's the probably like we need to push that to the database I still don't know what database I'm using so where all this information is being stored to okay
interesting so now we have this register and login page and I'm wondering then maybe is that coming from the root okay yeah that's that's coming from the root so maybe now if I in my page controller I don't want the app save that maybe I'm not saving it properly so if I say layout
false okay there we go so it's still going to use if I don't use a layout it's still going to use the I don't know the root layout so I can register okay yeah so like whatever that slash one so like maybe I'll maybe like I don't necessarily want that so the app is is is something that I want I just don't necessarily need all
of this um so maybe I'll just go ahead and just remove it for now there we go so we can create an account for josh.com okay create an account something went wrong log in no user verify okay module bcrypt is not available maybe I like forgot to do something okay anyway I I
still do have like the slash about so I I have all of that pulled in I I I I I understand it at a very basic level of course I'm not doing anything yet something went wrong when you're trying to log in we have the homepage we have slout perfect and now let's just see if we can like direct to it I I guess like
in the homepage do we have like a way of showing or maybe like in the in the app router like we have oh here we go so if okay we can direct to I wonder what this P thing is that's direct into user settings maybe that's just like the
root like the app do I need that so like if I was to go into the um about. html. heex and I was to say this is an href and I want to direct to go home like do I need this P about okay so yeah that that that does like a full page refresh it looks like but if I was to
do this go home and then in home I want to slash about so I guess it's it's the same I don't know what that dollar sign dollar sign p is okay so the Phoenix uh. gen. command generates a flexible pre-u authentication system so it uses live view or you can use live view I should say either their approach will create an
accounts context with an accounts. user schema model maybe mix. Ecto do setup wasn't run properly migration is already up migration is already up okay so maybe like I I maybe like I had have to rerun this okay CD Phenix start and then mix phoenix. server okay so we're back on to it let's do the register create an account okay there we
go I it's probably because I didn't like restart the server so we have settings see stuff like I I I I like this I like that you know that's the one of the reasons why I like Lille because I have authentication being able to be so easy for me um and we have log out we're logging
in welcome back I wonder how it's doing these kind of like popups so in like o or where is that pages so we have the page oh so it's in this live folder okay so user login I guess that would be
it okay socket assigns flash I'm guessing H so we have the live I'm guessing there's another file or another folder that is in addition to this um because when we have live wire you traditional structure you would have a view file and then you would have your your class file I'm guessing that's in a completely different spot so we have the
lib of so is this the migrations then the the schema is that technically the migrations it looks like it so like if I want to make a change do I just like run it again like how do you have migrations then us your login live okay so where is it pulling the um so I was like log in logged out
successfully like where's it where is it grabbing that logged out successfully session controller okay okay so yeah this is just something that just generated for me to be able to have this Flash but I imagine like that's that's that's how it would do it if I want to like show anything in the flash it just
automatically pops up I guess uh very interesting well I try to generate a new like live view template and see how that might work so we we kind of generated I mean we didn't generate I just created a new file and that showed like the about page uh but maybe we can generate a new templat so if I I wonder if they have like prompts where I like if I only have a blank they'll show up as more so I can Poss do
Phoenix or is it mix mix phoenix. live view um mix phoenix. gen. live does it like give me something okay no I have to actually tell it gen. lives that's really what I want of generated resource let's say I want like something associate to users so like if I want to
do like mix phoenix. gen. Live account accounts user uh todos accounts user Tod do let's just do that you're generating into an existing context Phoenix start accounts context currently has 19 functions three files
okay I don't necessarily know if I understand context like over how that's actually grouped we're just going to say yes um interactive overwrite let's go ahead and go for it mix Ecto do migrat let's just see okay so we created a table of to-dos that's somehow associated with this context of accounts and users now let's see like what actually happened here so we have
live user live show show user H maybe I maybe I like put it wrong because we have a show and an index and so I'm guessing this is like generating specific uh Pages for us like if we go to like users so on router we have so I probably didn't necessarily want it to be in accounts maybe or even like users okay starting from scratch just cuz I broke everything um so we'll go ahead
and call this instead of my app we'll still call this like uh Phoenix uh Phoenix Phoenix start I'm curious if I can see now what database it actually pulls up because this is just the the the the fresh from the beginning I kind of already know how to create the page but how do I get these live view Pages kind of up and running that's where I'm a little curious about it didn't actually configure database yet it
doesn't look like we configure it in config dev. EXs so if I pulled open this incode devex dev. EXs okay so this is where we're configuring our database and it looks like it's going to start with a okay so it kind of did for us we have a schema migration we just haven't necessarily ran it so if we do mix Ecto doc
create mix es was found in the current note that's because we are not in Phoenix start mix ector doc create uh has already been created okay so it's it's already been created now we just have to run the migrations but we don't necessarily have a any migrations yet and this is where we can do the the oth so we can do mix uh phoenix. gen
off okay mixs doget and we're going to do mix Ecto got migrate I'm going to stop that but then run mix uh phoenix. serve server phoenix. server mix phoenix. server okay so we should be up and running again refreshing okay we have the register and we're creating an account
we've got an account created perfect we have our settings we have everything everything back in the way it's good now what if I wanted to create Pages specifically to create to-dos for an account let's see so if we do like uh mix to our user we want to add todos with a name of string gen okay this makes a little bit more
sense because maybe we want it to be the US users now we're creating like this own users context because I believe like what I was doing before with this accounts user to do would be associating like we basically want a
user cuz how did we generate the how did we generate the actual um I think we want like user to do or users that's what it is so it's a users context now and maybe we don't have that context but now we do because we don't necessarily want to associate to accounts maybe or maybe it is just accounts and then to do or maybe it's just users so we'll
just say users uh to to do to do name String okay mix Ecto do migrate that seems a little bit more proper now we have this to do Pages no route found for good SL toos we
have live to-dos oh we need to add these okay so when we generate this it doesn't necessarily add them okay so we're adding all of this and then we're migrating but we already added I think we already migrated so in our a router is that where we do it add live routes to your browser and Phoenix start web okay so we don't even need to add it
to our there's no like live router we just add it here I think we add it here right okay so here's where we have like the different this is the live session so we're going to add it probably here but not in authentication routes but this does require an authenticated user so I'll probably add it
here so now we have todos but if I was to log out we can't go to todos right okay that's great must log in Access okay that's great I love that okay now we're going to create a new to-do first too G save the Tod do perfect if I log out and I create a new account um I'm assuming I'm still going to be able to see that to-do so Josh lal.com
slos yes we see it now how do I associate it with it um let's go ahead and create like go to the to-do the new index and to-do live this is where Elixir becomes a little bit more it feels very foreign to me when I'm looking at this I don't really know what I'm looking at um I know that an index this is the HTML but then we also have this index. ex so okay this is the this
is the the Livewire class if you want to call it that and then this is the actual view but here I don't know how to say I'm guessing here so users. list toos I'm just curious what Claud is going to say okay so this helps me out a little bit again I probably could find this in documentation but this gave me a little bit more context I suppose and I don't know the whole context and schema type stuff so this technically says you
generate a to-dos context which I didn't do that I just generated a to-do model so don't know if that's necessarily needed cuz here I'm I would assume like this is the context right you have a phoenix star you have a accounts and then the users have it to do but I don't necessarily know okay so again asking CLA it looks
like the user's context um it's is if you want to like associate it with outside of that given thing if I want to say maybe give me all the to-dos I can't say I have to say give me all the users to do which is interesting though because I I can do that on the page so I still don't necessarily know if I understand like the the context logic all right so I'm back I had to take some time and step
away to really get a grasp of what Phoenix and elixir brings but I think I have a little bit of a better understanding and that way I can kind of talk a little bit about at least in my mind what the key differences are on first grasp because of course I'm not really diving into a lot of intricate details of Phoenix and elixir all of this is just kind of like okay hey here's the things that I know don't
quite line up with my mindset for how you would build something within laral for example okay so usually what the key things I like to get a better understanding of and I haven't even touched things like mailing or uh cues or jobs or CRS or anything like that this is just strictly a database and authentication basically and how those models and uh resources kind of align so the first thing that I really didn't understand on first grasp was context so
I don't going dive into the code and I can talk a little bit about this at least from my new found understanding in the last couple hours so the biggest thing that I kind of didn't understand right off the bat was the difference in contexts because you in order to create and scaffolded out accounts you kind of or user in authentication you kind of have accounts and then accounts have a user and you know this isn't exactly the same within larl because you have just a
user model and so as best as I can understand within the context of larel to Elixir Phenix accounts is going to be the model as a whole maybe like the the service model where user is going to be actual like the user.php and so accounts might have things like uh maybe subscription or tokens of that
nature but it's not going to hold other models such as like to-dos um same thing with like a user wouldn't have its own context because a user doesn't have too much things associated with it it's really Associated to the account in this case we have a user Notifier user token kind of given to us out of the box so a to-dos is its own separate thing now this is the other part that's a little interesting because cuz you don't really necessarily have migrations for the most part at least
from what I can understand if I was to change a field I run this Ecto um I forget the actual name mix. Ecto maybe or mix. migrate Ecto do migrate that's what it is and then it will generate its own new migration but that's only if you're adding field so this is kind of like the source right here this to do. ex and this is where of course I can
have like casts um so I had this belongs to and the cool thing is if I generate this file using saying Hey I want a user ID that that is attributed or I forget the exact naming but in the CLI I created this and then I could just change it to this belongs to which is really similar to a I believe
a uh kind of relationship within larl and I do the same thing in the user. ex now we have this has many of the uh to-dos do too another weird thing is like you have the name of the project here so like this is probably Elixir specific of course but have Phoenix start that's my name of the project and then you have the to-dos context and then to-do same thing within
to-do you have the accounts context and then to-do so I think that's how you kind of use name spacing within Elixir again a little strange for me but I understand what's happening so I think the this Tod do. ex is more migrations and kind of like the the structure of the uh model whereas the
toos uh plural is going to be more the the the helper functions that you generate for the model itself so in this case if I wanted a list to do or a list user Tod do and this is where you would have I don't believe I changed anything in the users well there is no users it's just accounts but this is where you would
have kind of like what you would have within a user.php within Lille from again from my limited understanding so the one weird thing that really tripped me up a little bit in order to get this working for users authenticated to-dos in the sense that I only wanted these to-dos to be shown if you create them because um well well the first one of
being able to have uh in my to-dos here and being able to show um where did I put it being able to show or List users to do this all makes sense and this actually was scaffolded out for me with with co-pilot because then in my index index over here the index. ex um I
have this Mount and here is where I can say okay I'm getting the user token from the session and then assigning the current user and using this list user too stream passing in that user ID so that way I know that when I'm mounting
it I'm getting only I'm passing the to-dos to this uh index. html. heex but I'm using the list user to-do's method for lack of better terms um the the part that really tripped me up a little bit is in laravel I can use something like within any of my components regardless of how far down
they are in the tree I can say o user and then uh get their to-dos or like create a to-do that way o user to-dos create and then create it to-do with that relationship already intact because I know who's logged in and to my understanding at least from the AI that I talked to as well as just a little bit of Docker reading I don't think that's the way to do it within Elixir and that kind of tricked me up a little bit
because I was able to mount all the to-dos here but then let's say um one thing that was interesting is now I have to assign the current user to any socket that I open and this is probably specific to live view because I think each component is its own socket connection so I'm not I'm not sharing that state I believe and that's probably what is is hard for me to wrap my brain around is because it's just a new
websocket connection whenever you're doing something like listing the to-dos or creating a new to-do or editing a to-do and so I'm having to pass and assign a current user attribute to the socket's current user and so when I'm wanting to create a new too I can't just say okay when I create this in the form
component let's just create a new to-do in this case uh this is update so here here save where's create new to-do aha yes I did learn about this build association thing and I don't know if that's the right way to do this but that is a a way to say I want to grab the user but I still have to pass that user in so like in this case um the socket assigns
current user and then I can associate their to-dos that way but I think you so it's best or better maybe to just grab the uh user ID from the form itself in this case I'm passing the current user in the modal and then I just have that ID readily available for me so again not super clear on that but it's probably my own misunderstanding of how sockets and
everything works in that sense so it's more of like passing that around instead of having it be globally accessible because because a new webset connection might be opened every single time one of these things gets opened and so everything works great uh testing here and we can create it but if I was to log out and log in oh let's go ahead and add a too thing maybe in the in the root
maybe we can just say let's add a new link here of todos I still don't know exactly what this dollar sign p is I wonder if it's like so if I said users to do does it give me an error h no path okay yeah so it's Phoenix start web so like this is probably like the path itself okay so there we go need to log in first that's
why it's not showing it so we have Josh Lille there we go now we have to-dos perfect and this is a different testing to do if I was delete this log out and then log back into the other account we should still have a testing do and we do I can edit that complete okay so I understand a lot of like the structure of of separation of concerns
it took me a little bit but the the differences is mostly just syntax and at least in my mind of course there are some niceties that I am accustomed to like the easiness it's a little bit more verbose than I'm accustomed to overall I get why people love Phoenix because it's clean it's simple it's fast and and a lot of the apis and just things that it gives you out of the box are very much
Akin I think to batteries included Frameworks like rails or like laravel of course I'm still not quite grasping the idea of the functional style The Elixir brings and just the uh not just syntax the syntax is weird to me and I I understand it for the most part I mean I have to have C- palet fill in the blanks for me but the it seems like I'm running a little bit more code than I'm used to
but if this was something that I was learning from the get-go and I didn't have Lille or I didn't I would probably choose Phoenix over rails that's for sure but overall this was very much enjoyable I'm looking forward to being able to dive in maybe a little bit more and see what they have to offer in terms of in terms of things like mailers or or really where does it Shine within a web application because I know Elixir is
where you know multiple connections all at once the speed is really where it shines so when I have a problem that I can use for this to kind of learn more I'm really excited to dive in but regardless of your thoughts on Phoenix maybe you should just try it out see what you see what you think whether you're coming from lville maybe a JavaScript background uh overall just keep creating