1 00:00:01,240 --> 00:00:05,070 In this video we're going to try implementing option number three inside of our application. 2 00:00:05,590 --> 00:00:08,350 So we're going to take our query service down. 3 00:00:08,380 --> 00:00:12,370 We're then going to try to create some number of posts and some number of comments as well. 4 00:00:12,370 --> 00:00:16,120 We're then going to launch our query service and we're going to try to make sure that the query service 5 00:00:16,120 --> 00:00:21,360 can reach out to the event boss until it give me all the events that occurred up to this point in time. 6 00:00:21,360 --> 00:00:26,020 Now I want to remind you that we are putting together a homegrown version of an event bus. 7 00:00:26,020 --> 00:00:32,290 So there are real open source projects very production quality in nature that are going to do a lot 8 00:00:32,290 --> 00:00:37,000 of what we are discussing right here and a much more automated and well-intentioned fashion that would 9 00:00:37,000 --> 00:00:38,620 then what we are doing currently. 10 00:00:38,620 --> 00:00:41,930 So all you and I are doing are trying to get a sense of what is going on here. 11 00:00:41,950 --> 00:00:44,970 You should not use this code or about to write in a production environment. 12 00:00:44,980 --> 00:00:49,000 I just want you to have an idea of what is going on behind the scenes. 13 00:00:49,020 --> 00:00:53,610 The first thing we're going to do is to make sure that whenever our event bus receives an event we are 14 00:00:53,610 --> 00:00:55,830 going to store it inside of an array. 15 00:00:55,830 --> 00:01:00,990 We're then going to add in a new endpoint to our event bus an endpoint that can allow us to retrieve 16 00:01:01,080 --> 00:01:03,080 all the events that have ever occurred. 17 00:01:03,180 --> 00:01:07,890 Well then make sure that whenever we launch the query service we'll have it reach out to the event bus 18 00:01:08,280 --> 00:01:12,150 and request all the events that have occurred and we'll make sure that the query service attempts to 19 00:01:12,150 --> 00:01:14,460 process all that data. 20 00:01:14,500 --> 00:01:15,130 Let's get to it. 21 00:01:15,130 --> 00:01:19,120 The first thing we're gonna do is open up our event bus and make sure that we store all the different 22 00:01:19,120 --> 00:01:26,360 events that we collect over time so back inside my editor I'll find the event bus implementation series 23 00:01:26,360 --> 00:01:32,230 index dot J.S. Right above our post request handler where we deal with incoming events. 24 00:01:32,340 --> 00:01:39,400 I'm going to simply create a new variable called events and I'll have it be an array like so. 25 00:01:39,400 --> 00:01:45,440 So it's going to take any every single incoming event and throw it into this array then inside of our 26 00:01:45,440 --> 00:01:47,430 post cluster handler. 27 00:01:47,630 --> 00:01:51,900 We'll take a look at the event that we receive and we will push it into that array. 28 00:01:51,980 --> 00:01:59,950 We'll do it and events that push event now critically the most recent event is gonna be at the end of 29 00:01:59,950 --> 00:02:00,750 this array. 30 00:02:00,850 --> 00:02:06,490 The first event to receive will be at the very front of the array we're then going to create another 31 00:02:06,500 --> 00:02:10,600 endpoint on this event bus to retrieve all the events that have ever occurred. 32 00:02:11,410 --> 00:02:20,500 So after that existing post request handler I'll put in an app dot get or events with req and rez and 33 00:02:20,550 --> 00:02:26,670 then if anyone ever makes a request to slash events right here we will simply respond with that big 34 00:02:26,670 --> 00:02:28,810 list of events. 35 00:02:28,820 --> 00:02:31,860 Now again one repeat what I said just two minutes ago. 36 00:02:32,030 --> 00:02:36,020 Real implementations of event buses are way more complex than what we have right here. 37 00:02:36,020 --> 00:02:41,270 This is just to give you a sense of this approach of trying to synchronize services after they've been 38 00:02:41,690 --> 00:02:46,940 out of service down not even a known existence or whatever else OK. 39 00:02:46,970 --> 00:02:49,850 So now let's go over to our query service. 40 00:02:49,930 --> 00:02:51,340 I'm going to close that file. 41 00:02:51,440 --> 00:02:56,430 I'll go over to the query service and open up the index dot J.S. file. 42 00:02:56,590 --> 00:03:00,820 The first thing we're gonna do inside of here is extract some of the logic that we have inside of our 43 00:03:00,950 --> 00:03:01,860 post events. 44 00:03:01,860 --> 00:03:07,480 Root handler right now instead of here we've got a lot of code that knows exactly how to deal with or 45 00:03:07,480 --> 00:03:12,200 handle any given event that's all localized directly inside this post request handler. 46 00:03:12,220 --> 00:03:17,470 Right now but we're going to need to very quickly process events in some different locations inside 47 00:03:17,470 --> 00:03:18,460 this file. 48 00:03:18,490 --> 00:03:22,900 Long story short we're going to take all these statements and extract them into a separate helper function 49 00:03:24,180 --> 00:03:25,070 inside this post. 50 00:03:25,080 --> 00:03:27,990 Events are going to find the first if statement. 51 00:03:27,990 --> 00:03:30,630 I'm going to go down to the bottom if statement right here. 52 00:03:32,050 --> 00:03:37,990 I'm going to cut all that and so I'm just left with type data a console log in the send right there 53 00:03:41,030 --> 00:03:46,010 I'm then going to define a new function right underneath posts and I'll call it handle event 54 00:03:48,770 --> 00:03:52,640 and then I'm going to paste all the stuff that we just cut inside of that function 55 00:03:55,780 --> 00:04:00,370 now this function is going to need access to the data of the event and the type as well. 56 00:04:00,430 --> 00:04:05,440 So I'm going to assume that we're going to provide the type and the data as arguments to handle event 57 00:04:07,580 --> 00:04:12,650 then finally back inside of our post your quest handler is right here going to delete that console log 58 00:04:12,680 --> 00:04:19,700 which we don't really need anymore and I'll replace it with a call to handle event and I will pass in 59 00:04:19,790 --> 00:04:26,830 the type and data so all we just did is extract all that you've been handling data into a reusable function 60 00:04:28,880 --> 00:04:29,170 OK. 61 00:04:29,240 --> 00:04:35,210 So now let's say we need to do whenever our query service comes online and it starts listening on port 62 00:04:35,210 --> 00:04:41,180 four thousand into right after that would probably be a pretty good time to make a request over to our 63 00:04:41,180 --> 00:04:46,370 event bus and try to get a listing of all the different events that have been emitted up to this point 64 00:04:46,400 --> 00:04:56,190 in time so right after we do that console log I'm going to add in and await CEOs dot get we're going 65 00:04:56,190 --> 00:05:05,140 to reach out to each GDP call and slash slash local host four thousand five flash events that's gonna 66 00:05:05,170 --> 00:05:07,230 give us all the events that have occurred over time. 67 00:05:07,270 --> 00:05:14,200 Remember we're making a get request right here though assign that to some red variable I'm gonna make 68 00:05:14,200 --> 00:05:20,430 sure I also mark the enclosing function as a sync and then finally we're going to iterate through all 69 00:05:20,430 --> 00:05:23,250 the events that have occurred using a simple four of loop. 70 00:05:23,250 --> 00:05:25,830 We can also do a for each whatever you want to do. 71 00:05:25,860 --> 00:05:32,480 But in this case let's use for a very simple straightforward so say let event of red dots data. 72 00:05:32,520 --> 00:05:37,890 Remember whenever we use axis the response or the actual data we get back is available on the data property 73 00:05:37,890 --> 00:05:45,190 of this response so then inside of here maybe first we could do a quick console log and say something 74 00:05:45,190 --> 00:05:54,520 like processing event and then event type and then right after that we'll go ahead and actually handle 75 00:05:54,520 --> 00:05:54,970 the event. 76 00:05:55,000 --> 00:06:04,740 So handle event we'll put in the event type in the event data and that should be at okay. 77 00:06:04,750 --> 00:06:06,090 So let's save this. 78 00:06:06,300 --> 00:06:10,490 This is kind of a long video so take a quick pause right here and then test it out in just a moment.