1 00:00:01,320 --> 00:00:04,140 I'm inside of our publisher t s file inside of here. 2 00:00:04,140 --> 00:00:08,520 We are creating some piece of data and then publishing it off to the Internet server. 3 00:00:08,520 --> 00:00:11,950 Now when we take a look at this published code it definitely works right now. 4 00:00:12,290 --> 00:00:16,710 But I want you to really imagine all the locations where we could very easily make a typo and not really 5 00:00:16,710 --> 00:00:19,260 have anything to catch that typo. 6 00:00:19,260 --> 00:00:24,570 If I take a look at this data object I could very easily accidentally define price as a string like 7 00:00:24,750 --> 00:00:31,790 Dollar Sign 20 instead of Title I could very accidentally put in name on the subject of ticket created 8 00:00:31,790 --> 00:00:32,210 right here. 9 00:00:32,210 --> 00:00:36,460 I could very accidentally put in ticket created which is a different channel. 10 00:00:36,470 --> 00:00:39,150 As far as Napster misery is concerned. 11 00:00:39,150 --> 00:00:43,520 So even though this code is very simple right now and easy to understand there's an incredible number 12 00:00:43,520 --> 00:00:48,350 of locations where we could very easily make a typo that would be rather hard to catch when we start 13 00:00:48,350 --> 00:00:55,410 to deploy a service so in this video we're gonna start to put together a publisher bass class very similar 14 00:00:55,560 --> 00:01:00,870 to the base listener class we already put together the entire goal of all this is to get typescript 15 00:01:00,870 --> 00:01:06,360 to check our code and make sure that whenever we tried to emit some kind of event or some data it has 16 00:01:06,360 --> 00:01:10,890 the correct structure the correct properties the correct types and make sure that whenever we actually 17 00:01:10,890 --> 00:01:15,100 publish that data we provide a valid subject name inside of here as well. 18 00:01:15,270 --> 00:01:20,160 That is the goal of all the stuff we are trying to get typescript to help us out and make sure we don't 19 00:01:20,160 --> 00:01:21,730 make these silly typos. 20 00:01:21,960 --> 00:01:25,800 At this point just as a side comment this all might seem really unnecessary. 21 00:01:25,800 --> 00:01:28,680 You might be thinking Steven when am I going to make a typo. 22 00:01:28,680 --> 00:01:33,570 Well remember on a micro services project the kind of underlying assumption here is that you're going 23 00:01:33,570 --> 00:01:38,700 to be working on a micro services project with a team and so you need something in place to make sure 24 00:01:38,700 --> 00:01:43,560 that your teammates do not make mistakes just as much as you need to make sure that you are not going 25 00:01:43,560 --> 00:01:44,800 to make a mistake. 26 00:01:44,850 --> 00:01:50,460 It is very easy for people in a team to just very slightly mis communicate on whether some property 27 00:01:50,460 --> 00:01:52,550 name is title or name. 28 00:01:52,590 --> 00:01:54,260 You never know. 29 00:01:54,300 --> 00:01:58,620 So long story short we're gonna write out some code that looks very very similar almost identical to 30 00:01:58,620 --> 00:02:00,140 the base listener class. 31 00:02:00,150 --> 00:02:01,570 It's gonna be an abstract class. 32 00:02:01,740 --> 00:02:07,110 We're going to try to have some coupling or checking using a subject and some other kind of argument 33 00:02:07,120 --> 00:02:08,850 thingamajig or something like that. 34 00:02:08,940 --> 00:02:13,040 Let's just get to it and start writing some code inside of my events directory. 35 00:02:13,040 --> 00:02:19,160 I'll make a new file called base publisher dot. 36 00:02:19,190 --> 00:02:23,580 Yes inside of here I'm going to immediately add in a couple of imports. 37 00:02:23,590 --> 00:02:31,540 I'm gonna get Stan from node Nat streaming and I'm going to get our big list of subjects from subjects 38 00:02:33,740 --> 00:02:42,100 and then going to define and export an abstract class called publisher I want us to be a generic class 39 00:02:42,250 --> 00:02:46,570 because this is how we're going to eventually enforce some type checking on the subject for all the 40 00:02:46,570 --> 00:02:50,000 different subclasses a publisher we put together. 41 00:02:50,050 --> 00:02:53,560 So this is going to be a generic class we're going to accept some type that we're going to refer to 42 00:02:53,560 --> 00:03:04,150 as t and it's going to extend event let's define that event interface right above so in order to be 43 00:03:04,150 --> 00:03:09,690 an event you must have a subject and a subject must be one of the subjects listed out inside that subjects. 44 00:03:09,700 --> 00:03:15,580 Enum you must also have a data property and type can be any. 45 00:03:15,580 --> 00:03:22,970 We don't really want to enforce any specific data type around this based interface now inside a publisher. 46 00:03:22,970 --> 00:03:26,480 We're going to do something very similar that we did inside of our listener where we're gonna say that 47 00:03:26,480 --> 00:03:30,800 this thing has a couple of different abstract properties so we're not gonna use this publisher directly 48 00:03:31,160 --> 00:03:33,330 same way we did not use the listener directly. 49 00:03:33,410 --> 00:03:38,210 We are going to subclass publisher and have different publishers that are meant to publish different 50 00:03:38,210 --> 00:03:47,860 kinds of events so we're going to define an abstract property of subject and this must be equal to the 51 00:03:47,950 --> 00:03:56,120 custom type right here or the generic types subject property our publisher is also going to need a copy 52 00:03:56,120 --> 00:04:01,280 of the Nats client that hopefully has already been initialized inside our application so we'll make 53 00:04:01,280 --> 00:04:11,030 sure this thing has a constructor where we're going to receive some client of type Nats. 54 00:04:11,180 --> 00:04:18,150 I'll make sure that I update the client property and I'll make sure sorry not that Stan is such an easy 55 00:04:18,150 --> 00:04:22,710 thing to forget who came up with that they should really show is called a client but I digress. 56 00:04:22,710 --> 00:04:26,030 Also make sure that our class has a property of Klein as well. 57 00:04:27,100 --> 00:04:32,940 We'll say client will be of type Stan. 58 00:04:33,150 --> 00:04:36,690 I'm gonna make sure this thing is marked as private just so no one else tries to reach in and mess around 59 00:04:36,690 --> 00:04:39,200 with it at all OK. 60 00:04:39,220 --> 00:04:43,720 So now we need a function that is going to actually start to publish some information just as we had 61 00:04:43,750 --> 00:04:47,450 the listen function inside of our base listener class. 62 00:04:47,440 --> 00:04:48,550 No listen right here. 63 00:04:48,580 --> 00:04:52,310 This is what actually reached out with the Stan client and created a subscription. 64 00:04:52,360 --> 00:04:55,420 We want to have a function very similar to that inside of our publisher. 65 00:04:55,570 --> 00:05:01,240 And the goal this function is going to be to take some data or like an event data and publish it off 66 00:05:01,240 --> 00:05:02,940 to the Nat streaming server. 67 00:05:03,010 --> 00:05:08,990 So we're going to define a function called publish this thing is gonna take some data that we want to 68 00:05:08,990 --> 00:05:12,950 publish and its type is going to be up type T data 69 00:05:16,940 --> 00:05:22,200 then inside of here we're going to make use of our Nat's clients very similar to how we did back over 70 00:05:22,200 --> 00:05:24,270 here inside of our publisher t s file. 71 00:05:24,390 --> 00:05:27,150 We're going to call publish on that client. 72 00:05:27,150 --> 00:05:31,260 We're going to provide the subject that we want to provide this or publish this data as a then the data 73 00:05:31,290 --> 00:05:32,910 as a second argument. 74 00:05:32,910 --> 00:05:36,180 So inside of here we'll say this stock client publish 75 00:05:38,960 --> 00:05:40,930 the subject that we want to publish this event too. 76 00:05:40,940 --> 00:05:42,920 It's going to be the subject property. 77 00:05:42,920 --> 00:05:49,440 So this subject and the data that we want to be publishing is the data argument data. 78 00:05:49,610 --> 00:05:54,110 So now there is an optional third argument here of a callback function. 79 00:05:54,110 --> 00:05:56,840 We're going to come back and work with this callback function in just a little bit. 80 00:05:56,870 --> 00:06:03,440 But right now whenever it gets executed after we publish this event let's just do a console log of event 81 00:06:03,740 --> 00:06:04,700 published. 82 00:06:04,700 --> 00:06:09,430 And like I said it will eventually come back and do something a little bit more with the callback. 83 00:06:09,530 --> 00:06:09,750 All right. 84 00:06:09,750 --> 00:06:14,950 So believe it or not that is pretty much it for this abstract publisher base class. 85 00:06:14,970 --> 00:06:18,420 It's now just the same as what we're doing with our listener. 86 00:06:18,510 --> 00:06:21,110 This is the definition of what a publisher is. 87 00:06:21,150 --> 00:06:25,890 We're now going to subclass the publisher into something that can actually publish very specific kinds 88 00:06:25,890 --> 00:06:33,320 of events such as a ticket created event so inside of my events directory I'm going to make a new file 89 00:06:33,920 --> 00:06:45,080 called Ticket created publisher T.S. inside of here at the very top we will import the publisher that 90 00:06:45,130 --> 00:06:56,700 we just created from base publisher I will also import yet ticket created events from ticket created 91 00:06:57,360 --> 00:06:59,250 events. 92 00:06:59,360 --> 00:07:01,740 And then finally I'm going to get our subjects into 93 00:07:04,820 --> 00:07:13,000 from subjects that inside of your we're going to export a class called Ticket created publisher and 94 00:07:13,000 --> 00:07:20,390 it's going to extend publisher and because we are making use a publisher remember a publisher is a generic 95 00:07:20,390 --> 00:07:23,070 class which means we have to provide a type arguments. 96 00:07:23,090 --> 00:07:26,830 So the type of event that we're trying to publish here is a ticket created event. 97 00:07:26,870 --> 00:07:29,870 So we'll stick and ticket redid events like so 98 00:07:34,050 --> 00:07:36,770 then we're going to define the subject. 99 00:07:37,080 --> 00:07:43,190 If we hover over this as a matter of fact it'll tell us we have to define the subject very similar to 100 00:07:43,190 --> 00:07:44,210 what we did inside the listener. 101 00:07:44,210 --> 00:07:50,300 We're going to say the subject must always be of type subjects dot ticket created and the actual value 102 00:07:50,300 --> 00:07:55,590 from it must be subjects to get created and that's pretty much it. 103 00:07:55,760 --> 00:08:00,290 We do not have to define a publish function here we already define the Polish function inside the publisher 104 00:08:00,320 --> 00:08:02,510 based class. 105 00:08:02,580 --> 00:08:04,290 So how would we actually make use of this. 106 00:08:04,290 --> 00:08:05,760 Well this video is running a bit long. 107 00:08:05,760 --> 00:08:06,920 Let's take a quick pause right here. 108 00:08:06,930 --> 00:08:10,080 We'll come back the next video and then see how we actually make use of this custom. 109 00:08:10,110 --> 00:08:10,650 Publisher.