YouTubevideo· Sep 5, 2024· 10:11
Let's say you have a layer of a live wire application with some pretty intensive background jobs. How would you check on the status of those jobs and display that to the user in at least close to real time as possible? Now we're not going to be touching real, real time, like with something like Reverb, but even with a pretty generic implementation with polling using wire pole, can we show the user what's happening behind the scenes without just assuming we know what's happening? So you have a pretty intensive background job.
When we create a podcast, let's say it needs to edit that podcast. And then we're, publishing it to the different platforms. And then finally we're creating it on our side and maybe publishing it to a different social media places. There's a bunch of stuff that needs to happen behind the scenes. Well, usually if you're building something like this out, maybe the user doesn't need to know every single step of the process. Maybe we just assume, okay, everything is going to work out fine.
If it doesn't, then maybe we'll send an email showing that an error has happened, or maybe we can display that error later on down the road. And for the most part, All intents and purposes. Maybe we could just say, Hey, I'm creating a new podcast. And I want this to be called a live wire tidbits. And when we create it, it's processing everything happening behind the scenes, but maybe we don't necessarily know for sure if this has passed, maybe it's like a two minute background job, or maybe even longer.
And we're just saying, okay, Hey, we're getting this started. We're creating this now, this is all well and fine, especially for maybe most applications, but let's say there are a bunch of different steps. Maybe you have a specific status table in your model. Where you want to display to the user. Okay. Is it which step in the process? Has it been completed? usually when you're uploading a file, for example, you know, how far along in the process you are.
So can we show the user those steps what's happening behind the scenes? Just using wire pull and not having to get into any kind of like real time, maybe like live wire, reverb pusher type stuff. Well, we can, so this is going to be just a quick example. Let's say we have this, we have a podcast generator. Let's go ahead and say, maybe this is, my brand new live wire tidbits podcast. So we're processing this.
Now, what's happening behind the scenes is, this is beam processing. We're pulling the status, in this case, not just pulling P U L L, but also P O L L, pulling the status. And in the background queue We can see that status change. And of course this is just pulling the server every one second, but we're just waiting for the status to change. So it's going through all these status changes.
And when it finally publishes, when it finally finishes, we'll be able to see that down here. So how can we. Implement this. And also a quick little thing that I learned that we'll be able to show is how can we stop it from wire pulling when there's nothing to pull in this case, how can we remove the div that's being pulled? That's being, that's telling the server, Hey, I need more information without Any at least too flashy type of implementation.
Let's dive into some code. Okay. Here's the background job that's actually running. You can see here that we have some statuses that we're just looping through. This is just simulating what might happen with a long queued background job, but as we update this podcast as status, how can we display it to the user? Maybe, so this is the beef. For currently this is just a million. Hey, it's five seconds. It's probably created. We're creating the podcast.
Everything's good to go. You might think, okay, I guess the first step would just be creating a div and checking the podcast status and. You're correct. Like we could create a div here and say, okay display the status of the podcast that we created because we have the creative podcast. Now we're just checking that status in real time using polling. Yes, that's correct. But let's take a look at the implementation I have here.
First, we need to make sure that we have a create a podcast to poll. And if we set it as no, when the component loads, then we have something to start pulling, we can set the status. And then here's where I found actually interesting. And it's something that I've been looking forward to implementing in some of my own projects that I think this works fairly well, if you know of a different way. Let me know for sure.
But right here, we're just bringing all the podcasts where the pub, where the value is published in that enum or enum. And then what we're doing is down here. If there is a created podcast, let's display this div because live wire is blade.
Because blade you are live wire uses blade and because blade is server rendered, we have the ability because if you don't know about live wire, anytime any of these functions run, the component is reloaded in that sense, all of this blade is re rendered.
So when we run something like get status in this case or when we run create and process podcast, for example, this automatically sets the creative podcast. This variable, and then the component can reload, giving us the ability to show this div. So now we have this created podcast variable being server rendered.
So it's not pulling behind the scenes unless it has been set and we can start getting the status of the podcast. And of course this function just tells it, Hey, we're just pulling because wire pull has the ability to say, I want to only pull on a specific method. Okay. That's what we're doing here. We just want to run this method. We don't care about, running the width function or running like a mount function if we did that. And of course this is really rudimentary. You would probably want to have this.
This doesn't account for if you refresh the page, you see the status. You would probably want to redirect to a specific podcast show page like podcast slash ID. But this is just saying.
If we have a created podcast, in this case, if it is not null, then we're just going to refresh in this case, refresh this status of the podcast and grab it and make sure we're showing that to the user. So a couple of things I want to point out here, one, the ability to be able to dynamically set a div that has wire pull. I think this is incredibly helpful.
And it's also something I'm kind of pondering if I should send the APR. To live wire, because there's so many times where I only want wire pull to be dynamic and variable. I don't want to wire pull in every situation that the component is loaded. So, it would be nice to have like, Maybe you wire pull dynamic or something like that or wire pull get status.
And then maybe it's like wire wire pull Alive equals and then I could say created podcast. That'd be really interesting, right? so I'm yeah I'm just talking this through because I've been thinking of Easier ways to do some of the things that I'm experimenting with in some of my projects and I think this might be a great Live wire PR.
Let me know again if I'm doing something You Weird, or maybe there's a different way to do that, but this would kind of take the place of the, if created podcasts and if, because that would be, that'd be nice to have to say basically, Hey, I only want this to be pulling. And of course we probably, I wouldn't want to show the processing status anyways, unless it's pulling. So now that I'm thinking about it, that might not be the best way to do it.
But so hopefully this gives a couple of quick examples and I'll put the GitHub repo in the description below to check how I'm doing some things here. But a lot of you have asked, okay, how can we check long background running queues within Livewire? And yes, it's as simple as wire pulling, but you also have to know what are some gotchas?
What are some ways to do it without having to, set up reverb, or for example, or without just consistently pulling the server. So hopefully this gives you some tips and tricks. I think it works pretty nice and looks clean as well. That way we can just. Take the process that's happening in the background and display it to the user. Again, this particular implementation doesn't account for, refreshing this page.
It all happens just in the life cycle of the component. So a new podcast there's no refresh of the page. In this case, we're not redirecting. It would be probably better to, redirect to this specific podcast so that. On refresh, you could see where this status is.
In this case, it's only taking 45 seconds, but if it took 10 minutes, 30 minutes, that'd be a great way to, consistently pull the server only when you need to dynamically pulling the server while also still showing the user where you are in the process. Hopefully this gives you some tips and tricks. The next episode that I'm looking at is how do you use live wire to win wire navigate.
How are you using Livewire to grab URL queries and keep them or persist them across the page? I'm working on a project. I kind of need that. And it's really interesting. I have a couple of ways of doing it, but I'd love to get your thoughts.
So until then keep building, keep creating.