1 00:00:01,240 --> 00:00:01,530 All right. 2 00:00:01,560 --> 00:00:05,350 Let's get started working on this event bus implementation to get started. 3 00:00:05,350 --> 00:00:09,970 We're going to first create a new folder inside of our project directory to house all the code for this 4 00:00:09,970 --> 00:00:11,060 event bus. 5 00:00:11,160 --> 00:00:12,490 I'm back at my terminal. 6 00:00:12,520 --> 00:00:14,610 I'm gonna open up yet another terminal window. 7 00:00:14,640 --> 00:00:15,850 So I've now got four. 8 00:00:15,970 --> 00:00:19,610 Got one for the react up one for posts one for comments. 9 00:00:19,660 --> 00:00:21,750 I'm done here in the new one in the new one. 10 00:00:21,750 --> 00:00:27,200 I'm gonna change back to my blog directory which is housing all of my three overall services. 11 00:00:27,220 --> 00:00:33,250 Then in this folder I'm going to create a new folder called events we'll call it. 12 00:00:33,250 --> 00:00:34,290 How about event bus. 13 00:00:34,290 --> 00:00:41,190 Just be really clear then change into that directory let me make this when there's a little bit larger 14 00:00:42,430 --> 00:00:46,100 inside of here we're gonna generate a new package not just on file. 15 00:00:46,180 --> 00:01:00,400 So that's an NPM in it Dash y and then we're going to install express Node 1 access and I think that 16 00:01:00,400 --> 00:01:01,440 is about it. 17 00:01:01,450 --> 00:01:02,110 Yep that's it. 18 00:01:03,820 --> 00:01:05,980 We'll go ahead and run that again after this is done. 19 00:01:05,980 --> 00:01:08,500 I'm gonna open up my code editor inside this directory 20 00:01:16,670 --> 00:01:17,150 All right. 21 00:01:17,430 --> 00:01:23,760 So as usual we're going to create an indexed dot J S file inside of your at the very top. 22 00:01:23,780 --> 00:01:29,430 We're going to import Express. 23 00:01:29,650 --> 00:01:31,000 We're going to get body parser 24 00:01:35,090 --> 00:01:36,830 and we'll get access as well. 25 00:01:39,460 --> 00:01:41,690 And then going to create a new express application 26 00:01:44,950 --> 00:01:46,660 then associate the body parser. 27 00:01:46,660 --> 00:01:50,820 Jason middleware with this app so we'll do an app use body parser Dodd. 28 00:01:50,830 --> 00:01:56,180 Jason and then that's pretty much it for the initial setup. 29 00:01:56,180 --> 00:02:01,280 Now all we have to do is implement this endpoint to watch for incoming events but as we just saw in 30 00:02:01,280 --> 00:02:02,610 this diagram back over here. 31 00:02:02,630 --> 00:02:07,990 It's going to be a post request handler that's going to watch for a post request to slash events anytime 32 00:02:08,030 --> 00:02:13,160 we receive an event or it's going to turn right back around make a series of requests to these different 33 00:02:13,160 --> 00:02:19,130 servers so we'll set up a post request handler on slash events. 34 00:02:20,760 --> 00:02:22,880 I'm gonna put in my callback right here. 35 00:02:24,640 --> 00:02:28,870 We're going to get our event which we're going to assume is just gonna be the request body. 36 00:02:29,080 --> 00:02:33,190 So whatever comes along in the request body that is going to be our event. 37 00:02:33,340 --> 00:02:35,850 We don't know if it's gonna be a on object. 38 00:02:35,860 --> 00:02:37,030 We don't know if it's gonna be a string. 39 00:02:37,070 --> 00:02:37,590 No. 40 00:02:37,600 --> 00:02:38,740 Who knows what. 41 00:02:38,740 --> 00:02:42,910 All we know is that we're going to take whatever is inside that request body and send it off to all 42 00:02:42,910 --> 00:02:46,480 of our different running services. 43 00:02:46,520 --> 00:02:51,530 So now that we've got that event we're going to make our series of post requests to our other running 44 00:02:51,530 --> 00:02:52,430 services. 45 00:02:52,460 --> 00:03:01,900 So we'll do an axiom is not post to GDP all in Flash Flash local host or thousand flash events. 46 00:03:02,240 --> 00:03:05,120 And the data that we're going to send along is that exact event 47 00:03:08,650 --> 00:03:16,480 and then going to duplicate that line twice and I'll put in local Post four thousand one and then localized 48 00:03:16,790 --> 00:03:19,920 4000 to. 49 00:03:20,070 --> 00:03:25,510 And then finally at the very bottom we'll do a resort sent and we'll just say anytime someone tries 50 00:03:25,510 --> 00:03:28,590 to emit an event we'll just send back something and says status. 51 00:03:28,970 --> 00:03:34,270 OK just to indicate that everything worked as expected. 52 00:03:34,280 --> 00:03:37,040 Now there's one downside to the implementation we have right here. 53 00:03:37,040 --> 00:03:41,510 We are assuming that all three of these post requests are always going to succeed. 54 00:03:41,540 --> 00:03:45,620 We don't have any handling code around the possibility of one dam failing. 55 00:03:45,620 --> 00:03:49,430 That's just me I want to point out right now because it's going to start to become a little bit relevant 56 00:03:49,670 --> 00:03:55,380 as we start to implement each these different event and point handlers. 57 00:03:55,420 --> 00:03:59,290 So now finally at the very bottom we're gonna make sure that this thing is listening. 58 00:03:59,320 --> 00:04:00,500 So we'll put on an app. 59 00:04:00,520 --> 00:04:04,540 Listen we're gonna have this listen to four thousand and five. 60 00:04:04,540 --> 00:04:06,580 Notice how I skipped a couple of ports right here. 61 00:04:06,580 --> 00:04:12,300 So we've got our posts our comments soon to be our query service and then our event bus is just going 62 00:04:12,300 --> 00:04:20,130 to go directly up to four thousand five I'll put in my call back and I'll do a console dot log listening 63 00:04:20,160 --> 00:04:21,870 on four thousand five 64 00:04:25,530 --> 00:04:30,940 so that is it for very simple very basic event plus implementation. 65 00:04:30,940 --> 00:04:35,900 Now last thing we're gonna do we're gonna open up that package dot JS on file we're gonna update our 66 00:04:35,900 --> 00:04:40,560 script inside of here so I can delete the default test 1 and replace it with the start script. 67 00:04:40,580 --> 00:04:43,900 Once again we'll do node man index dot J.S. 68 00:04:47,070 --> 00:04:47,430 okay. 69 00:04:47,610 --> 00:04:50,130 So let's go back over George terminal. 70 00:04:50,280 --> 00:04:52,760 I've got my fourth terminal window right here. 71 00:04:52,800 --> 00:05:00,630 So again react those comments event bus I'm inside of the event bus and inside there I'll do an NPM 72 00:05:00,630 --> 00:05:02,570 start. 73 00:05:02,720 --> 00:05:06,290 And so I should see a listening on four thousand five. 74 00:05:06,290 --> 00:05:07,640 Very good. 75 00:05:07,850 --> 00:05:11,040 Now we've got our very simple event bus put together. 76 00:05:11,090 --> 00:05:13,030 Let's take a pause right here in the next video. 77 00:05:13,040 --> 00:05:17,900 We're gonna make sure that whenever someone creates a post or a comment we're going to emit an event 78 00:05:18,260 --> 00:05:22,310 over to our event bus so quick pause and I'll see you in just a minute.