YouTubevideo· Jun 7, 2024· 35:43· 3,000 views on YouTube

Even MORE Livewire and Alpine Optimistic UI Tips

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

88 moments
  1. so it's only been two months since I made my last video on optimistic UI and Lille Livewire kind of figuring out how Caleb porio did it and a tweet that he made I made a video about it you can find here uh but optimistic UI is still being talked about in the Twitter verse and just in the webdev world in general how do you make better experiences for users and since we're using larl Livewire Alpine JS how do you do that

  2. with those tools so the folks at Titan actually released a blog post on optimistic UI tricks for Livewire and Alpine I haven't been able to read it fully yet so I want to create a video give my thoughts on it as well as just learn something together on how can we do optimistic UI better for our users using Live Wire and Alpine so optimistic UI tricks for Live

  3. Wire and Alpine this is written by Tony Messias and Omar orita so follow them on Twitter I think I just started following them today just because of this blog post so was released today fresh off the press there's been a lot of debate in the last few weeks on the merits of building web apps with HTML over the wire versus using popular JavaScript libraries like View and react and how

  4. that choice affects the experiences of V users and developers in this post we'll talk through some optimistic UI tricks we apply when using laravel with live wire and Alpine to capture The Best of Both Worlds instantaneous feeling actions on the UI like what you'd expect from a single page app combined with the developer experience you may prefer in Live Wire and Alpine let's dive in I'm excited because I have loved live wire

  5. from when I started using it in Live Wire 2 and Live Wire 3 makes it even easier admittedly I am not an expert in Alpine I just kind of use it just because it's kind of mingled and interconnected with live and I know a lot of folks who do use it and I kind of just follow their example uh but even one of the earlier videos that I made was how to was ways to make a Livewire app feel like a spa a single page app um and I love those extra

  6. little UI tricks like with Live Wire wire navigate where you can kind of see when a page is trying to be loaded it's those little things that make users feel like on a modern app in my opinion so what's an optimistic UI optimistic UI tricks help us maximize perceived

  7. performance uh the perception of how quickly and smoothly Pages load and respond to user interaction is even more important than the actual time required to fetch the resources interesting while you may not be able to physically make your site run faster you may well be

  8. able to improve how fast it feels for your users that's a good point um it looks like this comes from uh um mozilla's page as well but that's a good point like even if your users have slow internet if they know that something is happening behind the scenes and you're actually actively telling them that that's why loading Spinners got to be so popular I I think you know we've kind of

  9. moved away from that we're like okay just do everything on the server but you still need to tell your users that something is happening you can rely on the browser for that but some browsers like some people just don't like the feel that it's it feels slow because my browser is taking forever to load the next page so that's why loading Spinners came to be of like okay we're telling the user that we're loading this information we're telling the user that something's happening they know that something's happening not just because the browser is loading but there's also something else happening and Live Wire

  10. can't help with that but I think we're going to get into this even more so starting from first principles getting to instant interactions is easy when there's no server simple counter serves as a perfect example try it bam yep as you can see that's just in uh I'm not going to open that up but that's just in the browser it's just the client so when you click the counter increments the UI is updated instantly

  11. no server is involved in the interaction and all the state is managed on the client side so now let's imagine that the counter has to make an API call to let the server know it has been incremented and then wait for the server to confirm that the increment was successful before reflecting the new state on the user's UI SO waiting for

  12. the server to respond okay I love that they're showing these examples this is becoming really popular in blog post which I'm all for um I'm not someone who likes writing as a medium first that's why I'm making a video about this blog post but I I love the extra thought and attention that it's put into a blog post waiting for the server to respond like if I was said like tried to clicking this multiple times you can see that like I clicked it like three times

  13. and it only happened once because we're waiting for the server to respond before it actually registers another click so this adds a few hundred milliseconds of lag to the user's perceived performance it's important to note that the slow slowness here isn't because we have a server receiving API calls it's because we're waiting for the server to respond before updating the UI what if we didn't wait for the response and instead optimistically assume that the request would succeed that's an optimistic UI

  14. okay so that's a great definition there and if you don't know what optimistic UI is now you know it's saying you know we're waiting for the server to respond but we're going to update the the the the client as if the server has already responded and the optimistic part is we're just saying hey we're not going to assume that it's going to fail 90% of

  15. time or whatever that percentage might be for the task that you're doing you know 99% of the time it's probably going to succeed if you're you know you're you're removing State let's say you're deleting an email email for example you know most of the time you

  16. when you click on delete it's going to succeed most of the time unless there's some weird error like you lose connection or you know the server's overloaded whatever that might be so why not just show it being deleted before you actually get a response saying hey this actually was deleted okay so ultimately an optimistic UI aims to minimize the client's Reliance on the server's response so that user interactions feel instant so sync in server State okay yeah so that it's I'm

  17. clicking multiple times Sy server State 22 I'm actually curious what this one is doing here so let's take a look simple counter um with JavaScript so it's basically saying uh increment local counter immediately clear the previous timeout if there's any and then set timeout to simulate the API call

  18. okay I get it okay so it's basically you know waiting for that t the the click to stop happening and then after it's it's it's storing all of that and then it's pushing it okay so so let's move on to a real world example using Live Wire and Alpine awesome I love this real world

  19. I'm curious okay we run a form as a service product okay that's awesome I didn't know the Titan Titan team did that so applying optimistic UI I'm reading ahead applying optimistic UI techniques in Live Wire and Alpine we run a form as a service product for forms called field goal the app initially used view on the front end but we recently migrated it to use live wire and Alpine I'm actually curious other than maybe they um I don't know too many

  20. people on the Titan team you know closely but I'm curious as to what was the decision moving from V I assume with inertia on the front end to live Y and outp maybe just to try it out just to learn new things um but yeah it's I I love what we're I love what we're diving into okay so if you haven't checked it out check out field goal um by the Titan

  21. team awesome so things worked well but the ux wasn't great on the inbox screen so we are trying to stay on the server side as much as we could I that's one point this point right here we are trying to stay on the server side as much as we could that's one point that I really love about Livewire because I'm very I I was a front-end person first I did JavaScript I did nextjs I did react I did spelt view even a little bit I

  22. loved doing as much stuff as I could on the client and not the other way around when I moved into Lille when I moved into Live Wire it kind of forced me to say okay what actually do I need to run on the client and I don't do a great job at this but Livewire also helps say do you actually need a Livewire component for that do you need to interact with the server or can it be done through a separate page does the form need to be on this page and receive information or can that just be a separate page all together something like let's say you're

  23. editing a blog post do you need to edit that in line in that sense receive information make it editable and then save it without leaving that page or can it just be a different page altogether it kind of forces you to think how much do we need to be stay on the server versus what can we what little can we do with a server you know uh but we pushed it too far to the ACT to the point the actions felt noticeably less Snappy okay those few

  24. hundred milliseconds of network cost for each action were hurting the user experience so we set out to tweak the implementation I have this in a couple of my products that I have as well I have loading spinners for example and sometimes I don't even have loading Spinners but if I was to click a button I have to wait for let's say I have this um you can set the order in one of my products you can set the order of what each is displayed so I have little arrow buttons on the side that move up each

  25. element let's say there's five elements if it was on four instead of clicking and dragging it's just little buttons it was my way out of not having to right click and drag um and so if you click the button you have to wait a little bit before it actually moves into the fourth position it's not the best but it's not the worst either I guess um so it looks like this say say animated picture or not okay

  26. users of field there we go users of fi go off and go through their inbox and archive form submissions they've already responded to or Mark a submission as spam if it somehow got through our spam filter so this is what that interaction looked like on a local machine throttling to emulate a fast 3g connection okay so they're like clicking on archive multiple times you have to wait until like it actually is

  27. completely deleted okay yeah it's easy to notice the lag between clicking the archive button and the UI arriving at its final State end State along with the fact that the transition isn't as smooth as it should be we wanted to focus our energy on optimizing the inbox ux so that taking an action like archiving a submission would make the item instantly disappear from the Inbox and then immediately Focus the next item in the inbox okay so

  28. it's not just remove or archiving it now it's also focusing the next item which is a little bit trickier as well so here are some tips and tricks we apply to improve the perceived performance of the app optimistically updating the UI using wire loading tip number one so Live Wire

  29. allows us to apply some loading States this is what I've showed in my in my optimistic UI video on Live Wire whenever specific actions are triggered one of the loading State options allows us to remove an element while Live Wire action is in progress by adding a wire loading do remove to the element we want to hide optionally we can specify the target actions that this Behavior should apply to with the wire Target attribute our first attempt to improve the inbox UI was inspired by Josh Siri hey that's

  30. me I've never been in a blog post before this is really cool um especially to be like next to Caleb porio in a blog post I I'm I'm kind of speechless okay um I wonder if this is to my to my tweet yes looks like it is um yeah so so

  31. Caleb porio tweeted out this thing of like hey here's optimistic UI without having to touch JavaScript because previously what I was doing with optimistic UI was um kind of sinking that client side with data on an Alpine object and then um you know with timeouts basically pushing that back to the server behind the scenes but this wire loading remove was an optimistic UI trick that I kind of showed a video on of use the remove element to take to

  32. optimize it based off the optimized way that the state should look and then Target it based off that thing so if we you know if this this was removing a email then we're archiving it on that click during loading so remove that

  33. email during the click and Target the specific email that was actually clicked to Archive I'm guessing that's not going to work specifically for them if I had to think ahead I'm not I'm guessing it didn't work specifically for them because there's one other extra element that they need in the sense of they're now probably focusing onto the next

  34. email on the list and that also triggers a whole new state so you're kind of having to do multiple things if I had to guess so our attempt looks something like this for each submission that's submission um wire key uh you know wire loading remove this based off of it's moving to archive although this helped and would have likely been sufficient for a simpler use case yeah it the the wire loading remove is is very simple in

  35. its nature in the sense that you it's it's specific for things like that is that element is now removed from the screen or we're adding a new element to the screen although s would simpar use case

  36. it wasn't enough in our particular scenario we could see the archive submission disappear ing from the list instantly but the next submission wasn't being automatically selected I am kind of proud of myself for getting that right we applied the same trick to the detail section and the story is similar users put see the empty box until they get a response from the server with the next item selected on the list and its details the issue here is that the selected submission logic happens on the

  37. server side okay yeah also we have multiple actions and Live Wire doesn't support targeting multiple actions with prams using wire Target at the moment I bet they said at the moment because if Caleb porio was to read this blog post he probably just because he's insanely smart and talented just is going to find a way to make that happen so furthermore since users can achieve a submission and then undo the action there isn't an easy for us uh

  38. there isn't an easy for us to track the submissions which have been archived and unremoved them using wire loading uh these issues didn't exist in the view version because all the interactions were happening on the client side luckily we can fix it without introducing a new tool since Livewire now ships with alpine out of the box great and and um in my video I didn't

  39. figure this part out I might have I can't remember I don't I don't think I figured this part out in the video but Caleb mentioned how he uses Alpine to reset the to-do list um input box so it it was basic instead of having to kind of wait for the server to respond to reset the state he was just using Alpine to reset the state on click and I'm wondering if it might be something similar to this so on click of archive you can still remove that element but use Alpine to automatically select the next the next uh form in the

  40. submissions okay tip number two minimize Network calls with client side state um what was I going to say oh yeah one of the things that I I think is interesting here is that um within the view version all the sign all the interactions were happening on the client side you know that's typically for most people who are coming from the JavaScript world and haven't done anything you know server first that's typically what you would reach for is okay if we're archiving this let's remove it from the state and then after

  41. that's removed then we can push it to the server from the background where within Live Wire and just within any kind of like server first rendered application usually you think the other way around hey we need to remove this from the server so that it is removed from the client so it's just a different way of thinking through things and therefore different ways of solving problems like with alpine okay as we

  42. said earlier this page was heavily reliant on server side state selecting one submission was done with a live wire action that set the selected submission State on the back end then renders that component which display the details of the selected submission in the details section all that works well on local it feels magical take a look okay so we're selecting it um set the submission State on the back renders the components they selecting it and it's displaying the information in that page as soon as we

  43. added latency into the game doesn't feel so magical anymore that makes sense because it's it's waiting for that information to then be loaded so that they can display it over here on the side see form. show and then it and then it finally finalizes this isn't live wire's fault that's something to note um all of this isn't serers side fault it's not hotwire's fault it's not live wire fault live wire's fault it's not live views fault this is just what you what

  44. the problems that you need to solve end up being when you think server first we shouldn't be using server side state for this kind of interaction in the first place there you go we fixed this by moving the submissions and selected state to the client side so did we wire ignore that that's basically just telling it that we don't want to try to reender this component when this happens

  45. we're ignoring that component while the server side interactions are happening X data submissions and this is the great part about Alpine with Livewire is you kind of just have these um directives that link the two the two things together so this is just saying uh selected submission is null selected submission okay so basically whatever that's it's

  46. setting the state of submissions to this submission within JavaScript so it's saying hey grab this submissions from the PHP uh element the state that's being loaded in and then we have this uninitialized we can set selected submission equals submissions versus array or null and then selected submission um function is changing the submission

  47. so we're not even using intangle here okay yeah all the interactivity for selecting submissions including keyboard navigation oo interesting happens entirely on the client's side also we no longer rely on Live Wire rerenders here we're delegating that part entirely to Aline instead of using something like wire click to trigger server side actions like this so wire click select submission and then you're changing the submission in that state we now need to handle that in Alpine so um submission

  48. and submissions so X onclick select submission that's running the function that was initialized up here select submission okay and this move to Archive me method would be on the Alpine component and it would look something like this so if not selected submission return um then we can wire call this move to Archive so basically that way um

  49. this this wire call is basically being able to call any wire method without having to do some fancy thing of like doing API calls in that sense luckily Live Wire and Alpine mixed together well it's almost like they were built by the same person laughing face emoji okay selecting a submission now takes place entirely on the client side without waiting for a response and it feels super Snappy I love that because it's it

  50. it it it replaces the way of thinking so instead of having to grab that state and then render that state on the client Side based off of what you're selecting and that state living on the server side just move it all to the client and Alpine works great and I think the the

  51. the kind of unsung heroes here are this await wire call which I'm assuming um is the is that mov to Archive method but the selected submissions is all happening on the client side so it's Gathering that data from the server but it's just loading it when the component is actually

  52. loaded however the actions still wait for the network response before update the UI okay so let's see if we can improve that so actions still wait for the network response before updating the UI okay I that makes sense I think in terms of like archiving is what they're saying optimistically updating the UI

  53. through client side State Management the issue now is that to update the UI we're still waiting for the Livewire action call to happen in our case the result of these actions would be the same remove the selected submission from the list select the next one if any so yeah the archive feature they did have the wire call archive I wonder if they're going to kind of expand upon that the the await method we don't need to wait for the response here we can optimistically update the UI by assuming the action will work so so yeah they had the await

  54. wire call so that was basically just saying that move to Archive they didn't change this too much from what you were doing with just wire wire click now they're moving it to a promise so wire call move to Archive again it's

  55. incredibly awesome that we can do some stuff like this and an Alpine you know component if you if you will okay and then we can await the promise and yeah con promise and then await the promise that's it actually UI will be instantly rendered updated when the request is sent to the server since we no longer rely on the renders we can

  56. update the UI to reflect the intention of the action the user took on the submission this is a snippet of the actual code in our case we're also recording the most recent action taken to revert it by either clicking on the undo button in the success notification or pressing the keyboard shortcut but the idea is the same here's how it currently feels see that archive it's not actually completed yet and now it's completed so now it's actually removed from the server archive um submission

  57. will be moved back okay so they're probably doing it since they're keeping all that state on the client now if you to Archive it and then undo it it's just cancelling that that uh submission to un to Archive it to the server so now the

  58. server isn't going to be like Oh I'm deleting it or I'm archiving it and then I'm re bringing it back that's the benefit of doing it in the client for something like this because you can be optimistic about maybe the user is changing their mind or maybe they want to do three of them at once or something like that okay tip number four introducing artificial slowness oo this is an interesting one I'm curious about this can the UI ever be too fast yes after we

  59. applied these optimistic UI tricks to fal's inbox we found for example that if the user held down the archive keyboard shortcut for a bit too long they'd end up archiving multiple submissions since the action is instant that's interesting it's interesting that a lot of times you know we solve all of these

  60. problems for the user of okay we're doing everything on the server now we need it to be faster and kind of show them on the client that everything's happening and they can do it instantly sometimes instant is almost too much it's like this this gratification piece of uh now it's almost like too fast where it feels weird youever you've been on those websites where like you're clicking around and you're like this feels off because it's just too fast there's no like loading Spinners or like SL slowness as I'm scrolling or like you

  61. know updating information on the server so it feels like maybe it's not actually saving properly in my instance that's in my case I've been on several apps or websites where that's the case where I'm like you know I'm deleting something and it's so quick that sometimes I don't know if I'm actually you know I'm actually synced with the servers feels like maybe this is just all client stuff and if I was to refresh I would lose everything especially if you don't have any kind of information saying hey I was saved or like updated or any kind of prompts or visual notification like that

  62. okay after we applied these optimistic UI tricks to field goals inbox found simple yep okay and we need to slow things down just enough to add friction between actions luckily Alpine has two built-in modifiers to help us again because now you're all doing this in Alpine on the client side now we can use

  63. Alpine's directives instead of having to worry about wires pulling directives or wires debounce directives built the modifiers debounce and throttle both the debounce and throttle modifiers default to a 250 millisecond debounce or throttle interval respectively you can tweak this higher or lower by doing something like debounce do 300 milliseconds throttle 700 milliseconds we experimented with both options and chose to apply alpines built-in throttle at the default 250 milliseconds interval to solve this problem we decided against

  64. debounce because it adds the leading delay which makes it seem like the action is slower perceived performance downgrade okay but throttle added a trailing delay which means that the first action is still instant but the next action will face friction which is what we want interesting okay yeah xile

  65. and click. throttle as simple as that it's helpful to try the app and see how it feels after applying optimistic UI tricks sometimes as users we subconsciously expect a bit of a lag in a visible render to tell our brains that our intended actions worked great I'm glad we agree after

  66. applying some optimistic UI tricks you may notice some areas needing more explicit feedback to tell the users that their action took effect so tip number five choosing when not to apply optimistic UI okay yeah optimistic UI tricks help us maximize the user perceived performance and that's really important when we're creating a highly interactive experience we started this refactor because here we go here's a refactor um reasoning as heavy used as a

  67. field goal we weren't satisfied with how the inbox felt to us um or actually this refactor might be after they move to Live Wire this is what they they weren't happy with the optimistic uips on the other hand field goal has other pages that are much less interactive our settings and building Pages for example aren't particularly exciting or interactive could we invest time into making the change password experience seem faster through optimistic UI tricks

  68. probably but it's hard to justify that investment so we're very content to continue using live wire for the entirety of the pages with very little or no Alpine involved I love that and that's just what I was saying like I love sometimes you don't need to have everything be perfect is a weird word you don't you don't need to have everything be as fleshed out as you think it needs to

  69. be so every single interaction within your application for things like a change password flow doesn't need to have those optimistic UI updates but can you give your users a better experience for the things that might need those updates yes you can and the good thing is we didn't move out of the tall stack so this was you know I assume Tailwind

  70. Alpine Live Wire and Lille they didn't have to add a different component or a different um element or a different you know they didn't have to reach out and go back into view for this specific element it can all be done with this it's just deciding which parts need to do it and then which parts can you remove from the server but still have the user

  71. experience the things that they need to experience an optimistic UI really shines with interactivity when you want users to get into a float State and spend a lot of time on a particular part of the app that's a great place to invest in optimistic UI I agree but it's okay to keep it simple with just SSR for the more boring bits where the user doesn't get more value from the optimistic UI experience conclusions and

  72. I guess like a piece here is like yes if if your whole thing was archiving form submissions which that's probably like a good part of it because you want that to feel Snappy and as fast for their users and even the users of their users because you're like okay I want to send this out to my users as a form I want it to feel fast so if it feels fast to me I'm going to feel comfortable sending this to my users to collect submissions but I doubt you're going to keep users or even gain users if your

  73. reset password is just a hair faster or it appears faster or even the login page like no one really cares because you're you're you're you're in an app because the app solves the problem that you want so spend time on the problems that it actually solves and and those and how those problems are interacting with your

  74. users so conclusions there are many different approaches to building user interfaces on the web and each approach has its own trade-offs and usefulness in a given situation JS and HTML over the wire have both seen a lot of success in the real world and they each deserve their own well-earned props however they can be even more powerful when wielded together can I get an Amen in the

  75. chat optimistic UI offers us a way to maximize perceive performance and create immersive experiences on the web we focused on how these tricks play out in Livewire and Alpine which helped us capture The Best of Both Worlds instantaneous feeling actions on the UI like you'd expect like what you'd expect from an Spa through Alpine combined with a developer experience you look for in Livewire that's a great that's a great

  76. tag that's a great tag that should be on like a t-shirt these tricks aren't unique to lville and Alpine either there we go optimistic UI Concepts can be applied to any stack whether you're building with view react hot wire or Live Wire and that's a thing that people I think are missing in this whole conversation is optimistic UI is easier

  77. in client side libraries you react spelt but that doesn't mean you also don't have ways of optimizing that UI from a server side in those languages as well where maybe what happens when it's been rejected or what happens when

  78. um you have you know you're sending multiple uh you know multiple events to the server do they get clashed um are you queuing up those Events maybe like within larl within Livewire it's a lot easier to say okay if someone is um saying I want to you know click on this button it's going to send an email now all of a sudden you can cue that up so it's not trying to complete that expensive request on the server before you're actually showing the user what happening that de balance is a great way of also you know making sure that that

  79. works properly and is displaying to users hey not everything is happening right away after that first one okay you don't always need an optimistic UI though and it sure isn't free one thing we didn't cover here is that you now have to handle some new edge cases like what happens when the action fails it might be easy to overlook these kind of issues and sometimes tricky to implement compensating actions when your app is focused on interactivity these optimistic U tricks can be a noticeable Delight to the user experience but it's

  80. okay to keep things simple for pages that don't need this level of interactivity like a settings page we hope you enjoyed reading this post drop us a line on Twitter at titano if you want to ask questions share more optimistic UI tips or say hi I believe I'm following them but I am you should follow them um as well as the whole team of Titan so Tony Messias um and I forget uh who else was the was was the author

  81. let's see um Omar Rita perfect so yes make sure you follow them follow the Titan team Matt staler who leads Titan big fan of them and their work okay where were we you want to ask

  82. questions share more optimistic UI tips or say hi shout outs a big shout out to Caleb porio and Josh Siri who recently posted content that inspired some of our internal conversations regarding uh optimistic UI I I appreciate that I'm still so shocked I'm like I'm placed next to Kaleb porio

  83. that's pretty crazy um but yeah follow Caleb poru if you have not obviously he's the author of this whole blog post technically because if Livewire and Alpine didn't exist then this blog post wouldn't exist so and my videos wouldn't exist so follow Caleb porio um Kio

  84. tweeted a demo of how to use wire loading remove as a way to instantly hide a to-do item that is marked complete on the client side without waiting for the server to respond I also made a great great video wow I I also made a video about this so you can find that find that here as well so thanks to much Titan team for for shouting me out I appreciate that yeah um read through this blog post if you are a building within within the tall stack within Livewire within Alpine there are some

  85. great tips here and some that I'm probably going to implement in some of my projects moving forward as well but the biggest thing that I I think that I like you know focusing on for stuff like this is when you are building in serers side components where you have Live Wire you're waiting for the server to actually complete things before you're actually showing it to the user you have to start thinking what things don't

  86. actually need to be on the server I made a video All About That where I said there's some things that you don't need to be on the server and sometimes we kind of conflate the two of everything should be on the client or everything should be on the server just like the blog post said it doesn't need to be that way JS and server side and all that

  87. stuff were written were created for a reason so just think about your users think about what kind of experience you want to give to them and then realize that there's no one way to solve all of that but you are giving your users the best possible experience that represent what your application is what you solve and how it can solve it for them then you're doing a good job so keep creating

  88. \[Music\] \[Applause\]

just you here