1 00:00:00,720 --> 00:00:03,630 We just put together our custom publisher before we test this out. 2 00:00:03,690 --> 00:00:07,830 Once you know that I made a little typo in the last video you're going to go back over to the base publisher 3 00:00:09,110 --> 00:00:12,380 right now we are receiving this data property right here. 4 00:00:12,380 --> 00:00:15,500 We're taking that data and we are passing it directly off to publish. 5 00:00:15,670 --> 00:00:20,250 But remember when we called publish on the Nats client we must be providing a string right here. 6 00:00:20,270 --> 00:00:23,910 We're gonna take whatever data is provided and turn it into Jason. 7 00:00:23,930 --> 00:00:27,630 We'll do a Jason string if I like so that was my mistake. 8 00:00:27,640 --> 00:00:31,780 I just left off that string ify function call. 9 00:00:31,790 --> 00:00:32,970 Now we've got that in there. 10 00:00:33,020 --> 00:00:34,930 We're going to figure out how to actually run this code. 11 00:00:35,510 --> 00:00:41,440 Well we're gonna go back over to our publisher test file inside of our SRT directory inside of here. 12 00:00:41,440 --> 00:00:44,890 We're going to import the ticket created publisher that we just put together. 13 00:00:44,890 --> 00:00:46,240 So that's this thing right here. 14 00:00:46,240 --> 00:00:51,010 And we're going to use this ticket create a publisher to publish an event at the very top I'm going 15 00:00:51,010 --> 00:00:55,100 to import ticket created publisher 16 00:00:58,330 --> 00:01:08,640 from events ticket created publisher and we go that downside of our connect statement right after we 17 00:01:08,640 --> 00:01:09,550 connect. 18 00:01:09,600 --> 00:01:14,490 I'm going to comment out the data variable and the publisher call that's already in there. 19 00:01:14,640 --> 00:01:19,080 I'm only commenting that out so we can do a comparison of how hard this code is to understand to be 20 00:01:19,140 --> 00:01:23,680 for and after the refactor so we're then going to create an instance of that publisher. 21 00:01:23,690 --> 00:01:28,560 So we'll say publisher is new ticket created publisher. 22 00:01:28,780 --> 00:01:30,570 We have to pass in a Nats client. 23 00:01:30,580 --> 00:01:36,310 So that's gonna be our stand variable that we had already declared and connected to Nats with and then 24 00:01:36,310 --> 00:01:40,870 to actually publish something we'll call a publisher publish and we'll pass on the data that we want 25 00:01:40,870 --> 00:01:45,570 to publish or this ticket created events as soon as I put in that empty object. 26 00:01:45,560 --> 00:01:47,910 Notice how typescript starts to show some errors. 27 00:01:47,980 --> 00:01:52,030 So typescript is doing some type checking for us it's making sure that whatever data we are trying to 28 00:01:52,030 --> 00:01:53,960 publish has the correct properties. 29 00:01:53,980 --> 00:01:58,530 So I.D. title and price and it's going to make sure that they have the correct types as well. 30 00:01:58,660 --> 00:02:04,270 So must be a string a string and a number this check right here is what it is all about. 31 00:02:04,290 --> 00:02:08,940 This is why we're going through all this extra effort to make sure that we do not try to publish some 32 00:02:09,000 --> 00:02:15,510 incorrect or invalid data that's then going to be consumed by other services so we'll put in a hard 33 00:02:15,510 --> 00:02:20,950 coded I.D. title of concert and a price of 20. 34 00:02:21,220 --> 00:02:24,610 And as soon as I have the correct properties and the correct types it goes away. 35 00:02:24,940 --> 00:02:31,320 And that means that we have defined this event successfully got time to test this out. 36 00:02:31,530 --> 00:02:35,280 I'm going to look at my editor make sure I don't have any typescript errors from any files all day to 37 00:02:35,280 --> 00:02:37,480 make sure I save all these different files. 38 00:02:37,530 --> 00:02:40,960 I'll go back over to our terminal and do a quick test. 39 00:02:40,960 --> 00:02:46,080 Now when you go back over to your terminal I would encourage you to just do a quick restart of the publisher 40 00:02:46,110 --> 00:02:51,450 and the two listeners just in case there were some air along the way that caused the connection we messed 41 00:02:51,450 --> 00:02:58,750 up or something like that so I'm going to restart my listener I can restart my second listener. 42 00:02:58,750 --> 00:03:00,520 It looks like those both booted up correctly. 43 00:03:00,540 --> 00:03:04,800 So if you get any errors chances are there's something wrong with the implementation right now I'll 44 00:03:04,880 --> 00:03:11,350 then go to the window that was doing the publisher and do a NPM run publish so as soon as I start that 45 00:03:11,350 --> 00:03:16,600 up I'll see the console log of 10 published I then see from one of the listeners that we received some 46 00:03:16,600 --> 00:03:22,630 event data and I got the console log in there of the event data itself and then the I.D. the title and 47 00:03:22,630 --> 00:03:24,650 the price awesome. 48 00:03:24,690 --> 00:03:28,730 So it looks like the stuff is all working OK. 49 00:03:28,860 --> 00:03:33,900 So this is definitely a fantastic improvement and I really cannot understate how important this refactor 50 00:03:33,900 --> 00:03:40,390 was even if it was a little bit painful and at times rather confusing there's still a couple of things 51 00:03:40,390 --> 00:03:41,680 I wanna show you really quickly. 52 00:03:41,710 --> 00:03:45,670 Couple locations where we can fix up some code and make it a little bit easier to use inside of some 53 00:03:45,670 --> 00:03:49,480 of our services in the future so quick pause right here and I'll see you in just a minute.