This episode is for members only

Sign up to access "Build An Uptime Monitor with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
38. Creating an endpoint down notification

Episodes

0%
Your progress
  • Total: 4h 59m
  • Played: 0m
  • Remaining: 4h 59m
Join or sign in to track your progress

Transcript

00:00
Now that we have the logic for whether we should notify a user let's go ahead and actually notify them now for this We're going to use events and listeners the reason being is that if we were to do everything inside of here
00:12
We would have to iterate through all of the email addresses that we want to send to We don't really want to do that in here and also if we create an event there might be another Instance in our application that tells us whether something has gone down So then we can just fire the same event and we'll get them emails sent out
00:31
So what we're gonna do is go ahead and create out an event here So let's go and use artisan to do that make event and we're just gonna call this very simply Endpoint went down. That's exactly what has happened Now if we just come over to our event service provider, let's go and register this
00:48
So we've already got one in here, which comes by default with Laravel breeze when a user is registered We send an email an email verification notification. We want to go ahead and add in our Endpoint went down in here Like so and we need an a listener for this. So what do we want to do when an endpoint goes down?
01:11
Well, let's go ahead and make out a listener here and we're gonna say send down email Notifications so this will iterate through all of the email addresses and send out to everyone So send down email notifications and we just need to put that in there
01:29
So whenever we dispatch this event, that's then gonna go ahead and call this now let's go over to our check observer and let's do this first of all, so endpoint went down and Dispatch now that's not to be confused with job dispatching. It's completely different We're dispatching an event here rather than a job now into this. We just want to pass in the check
01:50
We could pass the endpoint directly into this as well But we can extract the endpoint from the check so we can go ahead and grab it from that so in the endpoint went down event, let's go and accept in the check into the constructor and
02:04
That's done. And then down here. We want to get rid of broadcasting because we don't really want to broadcast this We could add that later. But for now, we just want this to be a very simple event with the check passed in so now what we can do is go over to our send email notifications and Under handle we can iterate through them and go ahead and of course
02:26
Send the emails to do this. It's gonna be pretty straightforward We want to go ahead and iterate through all of the notification emails on that array that exists within the site For that endpoint for that check. So we need to kind of go back twice in the relationship So we're actually going to go ahead and collect these up
02:44
So put them into a Laravel collection so we can very easily Doing each iteration on these a little bit efficient because we're going to iterate through and send out an email each time We're not going to send these in bulk But this kind of makes sense and we can also queue these as well, which we'll look at later
03:00
So let's go ahead and from the event that we get through that's going to contain the payload from event went down So that's going to have the check in there. We know that on our check model We have an endpoint relationship so we can go ahead and say endpoint and we know that on our Endpoint model we have a site relationship
03:20
So we can now go into the site and we can grab them notification emails Now we can go ahead and iterate through them so we can crowd closure in here and for each of them That is pretty much just a plain text email so we can send the email out in here so we're going to make sure we've got our email client open because when our endpoint does go down we want to make sure that
03:41
we actually receive these emails in and What we're not going to do here is use the mail facade to actually send an email to this email address What we're going to do is set this up with Laravel notifications So we can add more later
03:55
So we're going to use an email type of notification and then with Laravel we have support for things like SMS Notifications so we can add them on later Notifications are very powerful and we can route these through different channels So it makes sense to choose this for something like a uptime monitor
04:12
So we need to generate out the notification. So let's go ahead and do that. Now. We just say make notification and We're going to call this endpoint down Notification, of course, you can choose any name for that you want. So now that we've created this endpoint down
04:29
Notification we're going to go ahead and route that through mail But let's do a little bit of setup in here first of all so let's start the constructor here, of course for an endpoint going down we want this to Represent an actual endpoint. So let's bring the endpoint into the constructor here
04:45
So we can access everything that we need about that and then we have the different channels that we want to route this through So via it includes mail by default So, of course, we can just leave that in there and then we've got the to mail section here Which allows us to build up a mail message based on what we want to say
05:04
So let's go ahead and set a subject to this First of all, it's probably really important and we're gonna tack on at the very start here this endpoint location and of course I'm gonna keep this super rough
05:15
But you're free to go ahead and change around the copy and do whatever you want with this and then here we're gonna get rid Of these lines and actions. These are useful, but I much prefer to create out a markdown Version of this so we can construct an email really nicely directly within a kind of view So we're gonna put that in a folder called mail and we're gonna call this endpoint underscore down and
05:38
Into that we're gonna pass in The endpoint itself so we can access the data from the endpoint We're gonna review over all of this at the end of the video If this doesn't make any sense because we've introduced events listeners and now notifications
05:55
Inside of this episode. So now that we've done that let's go ahead and create the view We just basically want to see that this works first and then we can go over and review it. So under resources Let's go under views and we've got email in here. That was from earlier So let's actually change that over to email that kind of makes sense. And it's actually rename this to
06:15
endpoint down dot blade dot PHP and Let's just say endpoint Went down and we can actually just output
06:25
the Endpoint Idea as well just so we know that that's hooked up properly Okay
06:31
So now that we've done this inside of the our collection when we're iterating through each of them users We want to go ahead and send this notification Now the only problem with this is we have a list of plain text emails We don't have a list of users that we can actually notify if these were users within the application
06:49
we could just go ahead and say something like user and notify and then we could actually pass that notification in and But we don't have these as users. They're just plain text So a way to get around this is to go ahead and use the notification class and then route this through a specific channel so we're going to route this through the mail channel to this plain text email address and
07:12
That will pretty much bring us back to the point where we can then say notify and actually notify based on this and I think I've pulled the wrong thing in here. Yeah, this needs to be the facade. Let's just change that over At the top here as well, so let's get rid of this and We'll get rid of this. So this needs to be the notification facade. So let's go down and then let's notify this
07:34
so we're just going to new up the end point down notification so event and check an Endpoint so from the event we get the check and then grab the end point from that check And because we are working within a closure here. We just need to bring the event into scope here
07:55
Okay, so just before we send this off. Hopefully everything is good. Let's just go through what we introduced here So we looked at this check in the last episode and we are dispatching an event here Because this keeps everything nice and tidy plus we can dispatch this wherever we want and all of the stuff that follows This is going to be exactly the same
08:14
So this is the event that we've created and we register that over in the event service provider and we have the Listener here, which sends the email notifications to tell everyone that this went down that involves It's writing over all of the email addresses that we have stored for the site based on that end point based on that check and then it goes ahead and
08:36
through the email channel Notifies each of these plain text emails that the end point went down and this notification will handle Through the mail channel under this to mail method sending out this message We've built this up using the mail message class giving this a subject but using a markdown template
08:56
Which we're going to fill in a little bit later so let's now see if this works if we just hop over to the database just to Remind ourselves of our end point here. We've got a frequency of 10, which is good I'm actually going to return this back to forward slash and I'm gonna go ahead and get rid of all of the checks
09:17
That we've already performed just so we can start afresh Make sure there are no jobs in there and we should be good. So we know that to get this fired up We just want to first of all start our queue Then we want to go ahead and run our short schedule
09:32
Like so and then this should just start to go through and add checks so we should Eventually see a 200 roll in here Now this is where we want to keep an eye on our email because as soon as I edit this to be something that doesn't exist We should see them emails sent out. So let's just wait for a couple of seconds and sure enough
09:52
You can see we've got ABC went down and end point 10, which is the ID of that endpoint went down So everything's working nicely, but of course, we're only notifying one person at the moment So let's just test this out by sending this to another email address as well And we're gonna go ahead and edit this to work again
10:11
Wait for it to come back to the point where it is a 200 Okay so let's just keep refreshing this and wait for that to come in and there we go and Let's go ahead and edit this back to the point where it doesn't exist again
10:23
Done and wait for this to fail and of course now we should receive two emails one to Mabel and one to Alex So let's go over and there we go. So we've got one to Mabel here and one to Alex there So our endpoint down notifications are now rolling in and thanks to Laravel's notifications If we wanted to add SMS or any other channels later on this would be very very simple
44 episodes4 hrs 59 mins

Overview

Ready to dive into Inertia? Let's build a real-world app with Laravel, Inertia and Vue! If you're already working with Inertia, you'll pick up some tips and techniques for future projects.

This uptime monitor allows you to create and switch between sites, then add endpoints to monitor with frequency intervals. Using the power of scheduling and queues, it'll automatically alert the email addresses you've added via the UI when an endpoint goes down.

In this course, you'll learn to:

  • Build a real app from scratch with Inertia
  • Work with Laravel queues
  • Perform actions on models at user-defined intervals
  • Work with sub-minute schedules in Laravel
  • Send out channel notifications with Laravel
  • Use API resources to simplify Inertia data
  • Organise apps with events and observers
  • Create modals in Vue
  • Perform inline editing with Inertia forms
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!