YouTubevideo· Dec 6, 2024· 20:16
it's no surprise that the tool I reach for when I'm building applications is Livewire but are there pros and cons to Live Wire recently came across a Reddit thread asking okay what are the pros what are the cons for me I've been pretty open that I know that using Livewire isn't necessarily the best uh use case or the best tool to use for the job in every single example but it doesn't mean you can't use it for every single example too so why don't we dive
into this thread see if I think it's true or if it's not and what can we learn from this together all right someone said for the last 10 years I've been mostly working on the back and with the occasional dip into vanilla JS or jQuery with attempts at learning both react and view now that I'm unemployed I've been attempting to ramp those skills up the other day I started a tutorial on Livewire and for my money it seems much much better I'm curious as to your thoughts on using it over something like reactor view are there any
performance scaling debugging issues I need to consider how about anything else immediately my first thought so this was posted 4 days ago my first when I'm was reading this is it all depends on what your outcome is for me when I started learning live I already had a pretty good grasp of react um solid understanding of view and I can could use felt pretty decently as well but I
wasn't necessarily trying to get a job with any of those particular tools I just wanted to continue to learn and build things like SAS ideas or just content around a particular framework particular uh project and so if this person's goal is to get a job then I would say keep learning react within you know a lar of circumstance could be a
great option but Livewire necessarily isn't going to improve your skills in that particular aspect it will help you learn how to build things faster or think about things in a different way because now you're having to Think Through This Server client interaction more than you would have with react because something in the laral context like inertia puts that aside for you and so let's dive into what people uh answered with this the top one as of
right now is I use Live Wire extensively in an app almost all controllers are full page components and it's Snappy and responsive and that's usually what I would do as well I haven't the reason why I moved into Livewire and volt more recently is that I didn't have to write controllers I I it just doesn't work with my brain I I still use and can build things with inertia and I think it's amazing but controllers have just never you know worked with my head in
the sense that I know exactly how I want to build something and then I'm like oh yeah let me just write a controller and put this like cruddy by Design if you've never seen Adam wen's full talk I can't I think it was at a LaRon I could be wrong I can't remember now but it's cruddy by Design really great talk but my brain just doesn't work like that because probably because I haven't come from that MVC architecture until recently one thing which makes it a lot
easier is to remember to use Alpine JS whenever possible yes this is true but I don't think their example is completely true so you want to toggle the display of an element do something like this rather than this and I don't know this is true to some extent but not fully true it depends how you're toggling it uh the the actual show doesn't necessarily
matter this wire is enabled basically is mingling or entangling the wire server data with client data on Alpine to be able to show it but really if you're just kind of using a toggle to toggle something on and off that should be State that's kept in uh Alpine rather than in Live Wire so why don't we pull up some code all right it is correct that okay if I had like this Boolean of
show item and it's set to True by default and then we're just toggling item we have a button that toggles that item um then yes this is going to you know show the item and it's going to do a server side rendered but it also is the same if I uh did what they explained in that Reddit post as well so if I like refresh this page you can see here it's a server request because it's not
actually the showing of this item that really matters it's the toggle so those are all server side requests um if I was to grab the same code here like X cloak X show y enable if I was to to paste that in so we have x uh show wire. show item again this is entangling and it's doing that the same thing that we were doing before but it's still a server request you can see here we're
still making that server request what you would need to do is instead of wire click you would say something like at click or which is Alpine's directive to click but then you would have to use the wire item here wire. toggle item so if we were to do this refresh you can see here we're still doing a server side request now what's actually happening here
entangling this just like this item right here is not necessarily pushing this to the client this is just having the ability to talk or to send that state to your client and to keep it in sync with the server so if I was to you know if if I was to set this to false for example our
at um if is going to be server rendered where now this is going to automatically show and we shouldn't see this pop up for a little bit xcl should hide it actually but this should like pop up for a little bit while it's being rendered on the client and then this kind of uh syncs that with the server state so if I was to save this refresh you can see here we're still on
slow 4G um and as we go we might see this Flash and we did not and that is because we have this x cloak so technically this is being loaded on the client because we're X showing uh but this is really the solution to something like this this is where knowing the difference between the client and the server I've done a lot of different videos kind of talking
through this example but you need to know when you need to call something on the server and when you need to call something on the client if it's something that doesn't need to have a state set on the server then this should just all be done in Alpine what would that look like well we could wrap this uh X data here and we can say show item is false and then instead of our at
click toggling the toggle item that we have up here what we can do is instead just say uh this show item should be inverted we should say show item is equal to the opposite of what show item currently is and so now we can just say show items now what this is doing is pushing all this data into Alpine instead of on the server if this is
something that we don't need to sync to the server so if I was to refresh this and and we're still in slow 4G page is going to be fully loaded now the page is fully loaded if I click now all this is just happening on the client there is no server request now what you could do to entangle this is say x data show item uh is equal to wire. show item so now
you're saying that this data that I'm setting on the client is going to be equal to this whether it's equal to false or true and then and it's all dependent on how you're toggling that item so this Reddit thread is correct in the sense that the front end will be much quicker there's no round trip to the server for something like this but it also is dependent on how this wire is enabled is changing so saving this we
should see the same status so refreshing the page just to make sure it's a full refresh page refreshed show item show item that status is still being entangled because if I was to then change this to True show item up here again it's just being set to whatever this x data is
refresh we should after the page loads see this automatically without me toggling it and there we go but now we can set it open and close so this is helpful to have if you have some state that needs to be set but then Chang or adjusted or um reset on the client side afterwards someone said this is the correct response I do have a large SAS that is all live wire with multiple full
page components managing hundreds of sub components with additional inline components sprinkle throughout and it's Snappy as all get out I will say Live Wire V3 helped a lot with this because one of the biggest changes that's not talked about too much because it was kind of just more of a a behind the scenes kind of change but all server requests across any component on the stage are bundled together instead of individual components uh before I believe with live
live wire V2 the components were sending individual server requests so if you had four components on a page it would have four requests if you're updating a single piece of information across all components proper separation of responsibility between Alpine JS and liar components is also key this is the thing that I have always said is going to make the difference between what someone uh thinks of when they think of
Live Wire versus something like react within inertia or view within inertia because I think a lot of times you kind of see live wire and it's rightfully so kind of compared to something like hot wire or even HTM X and both of those things would recommend that you use something for the client side like Alpine JS but because live and Alpine JS are created by Caleb Borio the same
person a lot of of that magic is already included but you still have to know when to use the client versus when you're using the server Pros you can do responsive front end things with all PHP without touching one line of JavaScript um yes you still have to touch JavaScript well supported in many libraries and things to try out agreed crud is great perfect for simple crud apps I can disagree maybe a little
bit there it can be very chatty still basically long pulling s us a data either triggers too many updates or not enough this is probably more so just how you're setting up an application rather than in the Live Wire itself you can write bad code with reactors like you can write bad code with Live Wire when you get to really customizing things you will end up writing JavaScript onlyway yes absolutely if you saw my LC cast
course where we build a liveware app most of that is building albine JS there's a lot of client side stuff that I'm doing because you need JavaScript to do a lot of that you can't do some of that stuff just on the server I had some brick walls with data tables and end up going the inertia way for one project that is stuff and because what are data tables you push information from the server to the client and now you're having to
reorganize it on the client unless you want to make every interaction a server interaction if you're like filtering through data or you know like searching through pieces uh search is a little bit different but if you're filing through data or reordering most of that should be done on the client and I think even filament has started to switch a lot of that to client side interaction so filament uh does have tables that are
specifically built with live wire and Alpine JS so if I take a look at you know a table component uh yeah I believe most of this stuff is being in the table Builder I believe most of it is now using Alpine after it's being rendered it's a component that is given to you so you don't have to mess around too much you just kind of do the PHP side I would love live wire to be the perfect
solution for a single page site or something which administers a lot of data in one go but re-rendering was just too sluggish I ended up redoing it in Alpine and standard Ajax and it worked 20 times faster I'd be curious to know what this is that was replaced with standard Ajax again I think using react especially if you're looking for specific libraries or something like that or just a lot of abstraction taken care of for you when it comes to the
client side things is going to be better than trying to write it all with an Alpine or just vanilla JavaScript but the more that web components come into play the more that uh vanilla JavaScript comes into play even things like motion dodev which is now a modern library for JavaScript and react has its own vanilla JavaScript implement there's going to be so much easier ways
to do things within the tall stack within Live Wire and Alpine because there's going to be vanilla JavaScript implementations for a lot of the things that you otherwise would have to reach for a react abstraction 4 so I would bet and maybe this was in like live y V2 which I'm more familiar and am talking about live rv3 I would bet that most of this might have been done in live rv2
uh where maybe it was harder to see that separation between Livewire and Alpine because Livewire and Alpine were very separate in Livewire V2 it wasn't until V3 that they were more combined together and Alpine is part of Livewire now I'm guessing this is going to be more familiar to my experience so I was where you are back in the day it was Vanilla PHP with J Cory all the way well not for me because that was from the JavaScript side over time I started using larel
which I Lov but then my job took me another Direction while I was gone came the rise of angular react and view plus a whole heap of other JavaScript Frameworks including spelt Astro build Etc and there were others using node to run Direct on the server when my job change LED me back to web I was overwhelmed with options I tried many of them out and I quickly decide I would need some sort of full stack mean stack wasn't for me angular didn't interest me running a website with JavaScript and node was out it didn't fit with the team I was working with we all agreed the old
method of uploading via FTP and running a site with PHP was still the best way forward wow so I was left at PHP in some sort of modern decent JavaScript library because I had knowledge of larel I'll be at larel 4 at the time I decided to use that rather than vanilla PHP so the option was really Lille and either view which is well documented with Lille or react which was its own Beast so I initially went down the view line but I didn't quite like how much code was going into view it was almost too view heavy I wanted a simpler JS Library
someone on Reddit told me about Alpine JS it was so easy to use so straightforward and reactive but I also wanted an easy way to process the data back on the server rather than all through the client mainly for security keeping certain data away from the client that's when I discovered Livewire not only that but I discovered it goes hand inand with alpine JS so I went down the tall stack route initially I didn't want to use Tailwind but I decided to have it go anyway it turns out I really like it so tall stacking for me takes
every box on 95% of my Web projects I'm not interested in using anything else now it's a solid Choice the only negative Live Wire is it's a little slower than i' like it to be I'm hoping this might improve with time all of that makes a lot of sense and I think it really depends on where you're coming from and and then your approach to how you're building things as well if you're coming from a server side rendered you're going to automatically think okay
I know what needs to be on the client and I know what needs to be on the server and it's easier to see that way instead of writing your API routes you're writing Live Wire methods and then you know when you're calling something on the server um for for me I came from a very client side heavy JavaScript side and so it still was a little bit to kind of understand okay when am I actually writing something on uh the client or when should it just be on the client versus should it be on the
server I kind of a kin Live Wire and maybe volt in my own understanding to something more modern in the sense of like react server components Live Wire is great for crud is and great for complicated frontend apps no need V mpm uh be slow sometimes absolutely love live wire along with flux highly recommend Live Wire cons performance limitations JavaScript flexibility it's interesting how someone said a con was
you have to remember learn new security practices info and methods can be shown or called from the front end so you have to set up privacy and authorization as appropriate this was one that I actually had gave a point to Livewire when I was learning laravel with inertia and then Livewire because I kind of of uh started or really understood I didn't it took me a while to understand that um anything that I was pushing to the front and
within inertia was then just available where within Livewire yes you do have your methods available on the front end but data that you're pushing is just rendered through view so anything that you're not rendering you don't really have access to in the same sense that you would within an inertia application so detailing what I was pushing to the
server um had to be more specific within inertia rather within Livewire if I'm not um rendering that out in a blade template then I'm not actually showing it it made more sense my mind because what's on the page what I'm writing down is not or is what's actually being shown overall yes there are reasons to not use Live Wire is not going to be the perfect solution for every single use case but I think having or sticking with something
like that that might be more and I don't want to say this in the in a negative way but that might be more limiting in the sense that uh for front-end implementations or interactions usually there are options out there within react or view there's ways to do things that are already kind of set up in stone for you and then you just don't have to really worry too much because the glue that binds it all together with Lille would be inertia but the reason why I
love using live wi for every single project that I'm doing unless I specifically need to use something like react or view is because it forces me to then think through why am I doing something the way that I'm doing it why do I need to interact with a server here if I'm you know doing a um let's say a a voting button I need to vote on a specific uh thing or maybe it's just like a like or dislike button does that
need to hit the server probably but maybe it doesn't need to hit the server right away maybe I can uh you know do it on the client and then update it when they move away from the page or something like that overall I think there are a lot of things within Livewire that have gotten better over the years especially from Live Wire V2 to V3 and it's not going to be the same
experience or the same speediness that you might get with a full client side JavaScript framework like react but it does force you to learn learn how to use that bridge between the server and the client and thinking through how your building applications is really one of the biggest steps into knowing how to solve an application so keep building keep creating