1 00:00:01,580 --> 00:00:06,020 In this video we're going to start implementing class listener inside of our little test project now 2 00:00:06,020 --> 00:00:07,140 between the last video on this one. 3 00:00:07,160 --> 00:00:11,630 I want you to notice I added on another abstract property here of Q group name. 4 00:00:11,660 --> 00:00:16,550 So that means that we're going to have to define an actual value for Q Group Name inside of our subclasses 5 00:00:16,610 --> 00:00:18,590 of listener okay. 6 00:00:18,620 --> 00:00:21,990 So let's go back over to our Ed inside of our listener test file. 7 00:00:22,040 --> 00:00:27,050 We're going to define this listener class at the very bottom will eventually move this to its own separate 8 00:00:27,050 --> 00:00:30,440 file but right now this will work just fine down here. 9 00:00:30,440 --> 00:00:36,260 We're going to create an abstract class called listener. 10 00:00:36,340 --> 00:00:40,420 We're going to go through all the properties and add them in one by one along with some implementation 11 00:00:40,420 --> 00:00:41,520 for each. 12 00:00:41,560 --> 00:00:44,020 Let's first take care of this client thing. 13 00:00:44,080 --> 00:00:49,510 So we want our class listener to have a client property that should be a reference to a pre initialized 14 00:00:49,570 --> 00:00:50,690 Nats client. 15 00:00:50,740 --> 00:00:52,690 So it should be a type stand. 16 00:00:53,080 --> 00:00:58,120 When I say pre initialized I mean to say that we should have already connected to Nats successfully 17 00:00:58,440 --> 00:01:03,310 to the client we're going to pass in is essentially and be like that thing right there. 18 00:01:03,310 --> 00:01:05,050 After we have successfully connected 19 00:01:07,830 --> 00:01:12,700 so I'm going to assume that this thing is going to have a private property client and it is going to 20 00:01:12,700 --> 00:01:15,440 be a type stand a stand right here. 21 00:01:15,460 --> 00:01:17,670 As you'll recall is Nats backwards. 22 00:01:17,710 --> 00:01:21,020 This is what the Nats community calls clients. 23 00:01:21,230 --> 00:01:26,720 We need to import this Stan type from the node Nats library at the very top of the file so I'll find 24 00:01:26,720 --> 00:01:33,460 no not streaming and I'm going to get the Stan type and then back here. 25 00:01:33,460 --> 00:01:37,480 We now have an around client is just saying that we have defined as property but we never initialized 26 00:01:37,480 --> 00:01:38,090 it. 27 00:01:38,210 --> 00:01:41,920 Let's make sure that we defined a constructor function and we're going to make sure that the constructor 28 00:01:41,950 --> 00:01:44,690 receives a client that we're going to use. 29 00:01:44,870 --> 00:01:50,020 It's all defined the constructor will say this thing is going to receive a client property that is of 30 00:01:50,020 --> 00:01:54,120 type stand and we're gonna take that and assign it to clients. 31 00:01:54,120 --> 00:01:59,380 So this client is client. 32 00:01:59,400 --> 00:02:04,170 Next up we're going to make sure that we define a Q Group name and a subject. 33 00:02:04,170 --> 00:02:09,460 These are both going to be abstract properties which means they must be defined by our subclass. 34 00:02:09,600 --> 00:02:17,400 So I will put in at the very top of the class abstract subject like so that's going to have type string 35 00:02:18,510 --> 00:02:23,660 and abstract you group name that will be a string as well. 36 00:02:26,780 --> 00:02:30,110 Those are easy let's now take care of equate. 37 00:02:30,140 --> 00:02:35,990 So this will be the number of seconds we have to listen to a message and act it before the event. 38 00:02:35,990 --> 00:02:44,630 Eventually times out so I'm going to define this as private equate and let's actually make it protected 39 00:02:46,310 --> 00:02:51,860 instead which means that the subclass can define it if it wants to go into default is to be five seconds 40 00:02:54,180 --> 00:02:59,080 so five thousand milliseconds five seconds. 41 00:02:59,190 --> 00:03:03,780 Next up subscription options are subscription options and total are gonna be very similar to the options 42 00:03:03,780 --> 00:03:05,030 we had written out up here. 43 00:03:05,100 --> 00:03:06,800 We're just to find them inside of a helper method. 44 00:03:06,810 --> 00:03:08,830 Just have one convenient location for them. 45 00:03:09,680 --> 00:03:14,010 So define subscription options inside of here we go. 46 00:03:14,130 --> 00:03:26,260 We will return this stock client no client is our Nats library or Nats instance not subscription options 47 00:03:28,420 --> 00:03:33,750 you're gonna chain on a couple of different options to this thing versus say set deliver all available 48 00:03:34,910 --> 00:03:38,810 remember that's gonna make sure that the very first time our subscription is created we get all messages 49 00:03:38,840 --> 00:03:47,790 on this channel that have ever been emitted well then set manual act mode to true. 50 00:03:47,930 --> 00:03:51,870 We're gonna set that equate to this not equate. 51 00:03:52,110 --> 00:03:56,270 So that's how we customize the acknowledgement time out period normally 30 seconds. 52 00:03:56,270 --> 00:04:02,030 But again we're going to certain that down to five seconds and then finally we'll also set up our durable 53 00:04:02,030 --> 00:04:05,420 name set durable name for the durable name. 54 00:04:05,420 --> 00:04:11,210 We're just going to end up using the same Q Group Name so we're going to use the Q Group Name both as 55 00:04:11,210 --> 00:04:15,320 the Q Group Name and as the Journal name because it usually makes a lot of sense to just use the same 56 00:04:15,320 --> 00:04:16,280 one. 57 00:04:16,280 --> 00:04:20,440 It's very rare that we're going to distinctly want these things to be different. 58 00:04:20,530 --> 00:04:23,870 So for set terrible name we will pass in this dot Q Group Name 59 00:04:28,930 --> 00:04:29,420 after that. 60 00:04:29,440 --> 00:04:34,060 Let's put together our listen method to the goal of listen is to really just set up the subscription 61 00:04:34,060 --> 00:04:34,780 itself. 62 00:04:34,780 --> 00:04:39,520 Whenever we get an incoming message we're going to pass the message using a pass message method and 63 00:04:39,520 --> 00:04:50,280 then eventually call an on message method so inside of a listen right here we're going to first create 64 00:04:50,280 --> 00:04:56,970 the subscription itself so cons subscription is this client subscribe and we're gonna pass in those 65 00:04:56,970 --> 00:05:01,280 three arguments the first one is going to be the subject that we want to listen to. 66 00:05:01,370 --> 00:05:02,370 So this not subject 67 00:05:05,130 --> 00:05:06,870 the second is going to be our Q Group name 68 00:05:10,040 --> 00:05:12,900 and then third will be our actual subscription options. 69 00:05:12,920 --> 00:05:14,930 So we'll call this dot subscription options 70 00:05:21,360 --> 00:05:25,570 that will give us our subscription after we get it. 71 00:05:25,610 --> 00:05:26,700 We have to start to listen to it. 72 00:05:26,770 --> 00:05:30,910 We'll stay on message. 73 00:05:31,000 --> 00:05:37,490 We're going to receive MSP which is of type message inside. 74 00:05:37,520 --> 00:05:42,110 We'll do just a little bit of logging info just to say hey we just received a message so that we can 75 00:05:42,110 --> 00:05:46,140 more easily see when we receive messages and they're being dealt with inside of our app. 76 00:05:46,250 --> 00:05:51,200 So do a console log I'm gonna put a little new line inside of here because the log that we're gonna 77 00:05:51,200 --> 00:05:53,230 do is a little bit long. 78 00:05:53,350 --> 00:05:59,130 I'm gonna put in a template string so back ticks and we'll say Message received we're going to print 79 00:05:59,130 --> 00:06:07,350 out the subject of the message that we just got and maybe we should also print out the Q Group name 80 00:06:07,350 --> 00:06:12,110 for this thing as well just we understand exactly what group of services receive this message. 81 00:06:12,110 --> 00:06:15,270 So I will also print out this dot Q Group Name 82 00:06:19,350 --> 00:06:19,860 after that. 83 00:06:19,920 --> 00:06:23,580 We're going to want to pass the incoming message and pull the data out of it. 84 00:06:23,580 --> 00:06:29,330 So I'm going to really quickly define R's message down here. 85 00:06:29,630 --> 00:06:33,100 So remember when we get a message we have to call that get data method on it. 86 00:06:33,100 --> 00:06:36,050 That's gonna give us either a buffer or a string. 87 00:06:36,120 --> 00:06:39,790 We're going to make sure that if it is a string we just person data out of it and return the results 88 00:06:39,790 --> 00:06:46,890 of that so we're going to assume that we're going to receive a message in this thing of message. 89 00:06:47,000 --> 00:06:48,260 We'll get our data out. 90 00:06:48,470 --> 00:06:51,560 So message data 91 00:06:55,520 --> 00:07:01,680 then if we are working with the string or a buffer remember it can be either we mouse over data right 92 00:07:01,680 --> 00:07:05,400 here we'll handle it appropriately we're gonna use an attorney or expression to do this. 93 00:07:05,400 --> 00:07:08,820 So we'll say return type of data equal to string. 94 00:07:08,850 --> 00:07:13,800 So if it is a string I want to do either one thing or the other. 95 00:07:13,800 --> 00:07:16,020 So just a normal ternary expression. 96 00:07:16,080 --> 00:07:23,110 So if it is a string let's go ahead and do a J signed up pass of data otherwise if it is a buffer we'll 97 00:07:23,110 --> 00:07:24,070 handle it appropriately. 98 00:07:24,070 --> 00:07:25,580 Here's how we would handle a buffer. 99 00:07:25,630 --> 00:07:30,120 We don't ever expect to get a buffer but we're gonna write some code to handle it just in case we'll 100 00:07:30,130 --> 00:07:35,420 do a Jason pass data dot to string UTF 8. 101 00:07:35,440 --> 00:07:41,390 That's how we would pass a buffer and get some Jason out of it. 102 00:07:41,690 --> 00:07:48,240 So back inside of this subscription dot on we will call or get past data and that's going to come from 103 00:07:48,240 --> 00:07:57,780 this dot pass message and we'll pass on the message or excuse me MSE and then finally we are going to 104 00:07:57,780 --> 00:08:03,570 assume that there is some kind of on message event or me on message method that we want to call with 105 00:08:03,570 --> 00:08:05,910 this event data that we just passed out of it. 106 00:08:05,910 --> 00:08:16,550 So I'm going to try to call this dot on message with the pass data and just for consistency sake we 107 00:08:16,550 --> 00:08:18,040 don't really know what we want to do inside of. 108 00:08:18,040 --> 00:08:19,090 On message just yet. 109 00:08:19,100 --> 00:08:23,390 So in addition to the actual data that we're gonna throw in there let's just pass in the entire message 110 00:08:23,420 --> 00:08:27,990 the original one as well just in case we need to get access to some other property off the message itself. 111 00:08:28,010 --> 00:08:32,250 So it's and message like so we are getting an air out of on message. 112 00:08:32,290 --> 00:08:35,930 So we do have to define that as an abstract method on our class. 113 00:08:36,040 --> 00:08:41,770 And again by being an abstract method that means we must define it inside of a child or subclass. 114 00:08:41,770 --> 00:08:46,930 Well very quickly define on message at the very top by all of our other properties about by all the 115 00:08:46,930 --> 00:08:48,530 other abstract ones. 116 00:08:48,670 --> 00:08:56,710 So we'll say abstract on message and we're going to assume that this is going to take in some data right 117 00:08:56,710 --> 00:09:06,890 now we'll say that it's of type any and a message of type message and it's going to return void okay. 118 00:09:06,950 --> 00:09:10,460 So that is our class listener lot of code inside of here. 119 00:09:10,460 --> 00:09:14,570 But really all we did was kind of massage a lot of the stuff that we already wrote out. 120 00:09:14,600 --> 00:09:16,800 This is all the same stuff we already put together up here. 121 00:09:16,850 --> 00:09:19,550 We just kind of redistribute it around. 122 00:09:19,580 --> 00:09:20,510 Let's take a pause right here. 123 00:09:20,510 --> 00:09:24,860 When we come back next video we're going to take a look at how we can now subclass this thing and very 124 00:09:24,860 --> 00:09:26,180 easily create a listener.