YouTubevideo· Aug 15, 2024· 10:21
Every now and then I come across a blog post about a topic, whether in Laravel or LiveWire or just some other form of knowledge in the web development world. And I learned something new, whether that's from the blog post directly or getting me to try something out that I haven't tried before. And that's the case with this blog post about LiveWire polling. So why don't we take a look. So this blog post was written by Harris Raftopoulos about interactivity with wire pull keep alive.
And a lot of the functionality that Harris kind of described in this blog post is something that I wanted to make into a video. So go ahead and read this. The link is in the description, but why are pull keep alive? Essentially, if we take a look at the live wire docs. Wirepull Keep Alive allows you to still pull the server even when the page is open in a different browser tab. Let's say you wanted to have live updates of scores for sporting matches.
Well, if you have it open in a separate tab or maybe in a separate window, that page isn't necessarily active. Wirepull is not pulling in the background, it's being throttled. Live wire will reduce the number of polling requests by 95 percent until the user revisits the tab. This is all well and good for 98 plus percent of use cases. But in this particular use case, keep alive is incredibly fantastic.
And the good thing is wire polling automatically works incredibly well with any kind of filters. So I figured this would be a good video to kind of have a little nugget of how can you Use filters within LiveWire and then what are some additional options that you can take a look at within WirePole? Not just keep alive, but then you have things like viewport throttling, not necessarily relevant to this particular demo, but as well as specific timing control. So let's take a look at the demo application.
So let's say we have a list of live scores. First thing I want you to take a note at is the filters that we have are automatically applied into the URL. So we have the ability to look at specific favorites. mark favorites. We can add some new favorites. We can search and find specific scores that we want to take a look at and add them to our favorites. And all of this is happening while the scores are being updated behind the scenes.
So the actual database is being updated thanks to a, uh, PHP artisan command that I implemented, where this is consistently running and changing scores every five ish seconds. So therefore we want these scores to be reflected in the browser. And we can see that happening in the way LiveWire works is the component refreshes.
And every single time you make a server request to it, we're not having, we don't have wire pull actually added to our app just yet. But when I change specific filters, all of a sudden you can see here, this yellow highlight is showing scores that have been last updated in the last five seconds. change it again. You can see here that score has been updated because every single time LiveWire has a server request happen.
In this case, if I was to unfavorite a score, the component refreshes. In this case, because we're using LiveWire Volt, the with function is really similar to a render function. If you're used to using LiveWire specifically, the classic version, I guess I should say, anytime the component.
Renders in this case if any of these methods are ran if we toggle a favorite if we check Or if a filter is set in this case the state of the filter is set to the URL if a filter is set We're going to re render the component. We're going to take anything that's in this with function. In this case, the scores and all of these queries and re render. We're getting new information back from the database and that's great. Uh, but we want this to be more live.
We want to see scores in this case, most of the time a scoring app like this, you're probably just going to have it in another tab. And when you go back to the tab, you're like, okay, I want to make sure I'm seeing the up to date scores. I don't necessarily want to wait another five seconds when I click the tab to get scores and stuff like this is probably best done using WebSockets. So you get real time.
But when it's just text information getting sent back and forth from the server, polling is just fine, especially if you're polling in something like a 5 second increment or maybe a 10 second increment. In fact, even in American Sports for MLB, for example, their apps use polling. They poll the server to get information every 5 seconds. I think it might be 3 seconds.
But nevertheless, that it's not necessarily you need to know it right then and now, but as the information updates, we want to see it happen on the page. So again, all of this, let's say West, all of this is happening live in the browser. We have the search of West and filter of favorites. Filter of all, but how do we get this to be pulled in and brought in? And that's where wire polling comes in.
The good thing is we don't have to change anything about our code. Everything works like we would expect it to. We just can add wire poll and we can say maybe five seconds and we'll do every, and we'll do five seconds and that's where we can add this little modifier. Let's check and see how long it does by default. Every 2.
5 seconds by default, but we can change that with the timing control to seconds or even Milliseconds, but we'll do so we'll do five seconds.
And now if I was to refresh we still have at the filters that's one thing to keep your state in the URL because if I was to copy and paste this URL send it to someone or Refresh we have that information there Still, in our app, it's the state is in the URL and you can see here, okay, every five seconds, new information changes, new scores get updated.
And we see that happen due to this tailwind class that we have here, where we say. If this score is recently updated in AKA as the score been last updated in the last five seconds, we're just going to add this background of yellow. So you can see the scores being updated, the style being updated because the database is being updated, but that is happening behind the scenes.
So score five, three zero two, just changed to zero three. All this is happening with that artisan command that I created. But why don't we take a look at. What happens when I, if I was to go into the network, we'll go ahead. we have two requests, three requests. Let's change it. And I'm going to change to a different page because wire pulling does not keep moving in the background. I am not going to get consistent scores when I change back to our scoring app.
Let's take a look where there was, I cleared the network tab, so we shouldn't see anything until I take a look. It reduces it by 95%. So we might see one or two. Let's see. Only one in the 15 seconds that I was talking to you. And now we get the live scores. And again, this is probably not for every single app. You might not need this, but here's what keep alive does. Wire pull five seconds.
We'll do wire pull dot keep dash alive dot five seconds. And now we'll go back. We'll go ahead and refresh the page. We have this network tab. And okay, we get a new one. I'll go ahead and clear this and change the page now every five seconds. And let's say I talk to you, talk to the camera. As we're looking at this timing control, background throttling. Let's say this has been.
Around like five seconds, around like 10 seconds. So we should probably see it at least two or three requests to pull to the server. When we switch back to the tab again, wire pull keep alive just means when I am in a different tab or the tab is not active you can also use viewport throttling, which is awesome. It's still going to pull the server to get up to date information so that if I was to look back at the tab We get all that ready to go.
So taking a look back you can see here it's been pulling in the background for us and this is great because I think And we can confirm this right now, but I think if I was to have, uh, or have a different browser or tab active, even if this tab is still showing, it doesn't refresh if I wasn't using keep alive. So let's go ahead and clear, keep alive, and we'll go ahead and refresh.
Refresh, inspect, so we'll go ahead and sit here, refresh, and now I do not have the browser alive. I'm opening another browser. Okay. So it's still going. But maybe we were to have this. page in this tab here. And. Okay. So that's still going. So yeah, , it's only when the tab is not. And probably different browsers treat this differently, but it's helpful to know that there are ways to do this.
If. Live updates is something that is important within your application. So again, real quick nugget for you today of maybe some new things that you might not have thought of when it comes to filters within a live wire application, but then also pulling reduces by 95 percent if you have the tab not active. So wire pole, keep alive changes all that it's still pulling in the background.
Even if we're not looking at it. Some quick live wire tips and hopefully something fresh that you might have learned.
Keep creating.