1 00:00:00,920 --> 00:00:05,330 We've now got our abstract class listener which is going to make it a lot easier to create listeners 2 00:00:05,330 --> 00:00:08,970 for all the different kinds of events that are going to be flowing around our application. 3 00:00:08,970 --> 00:00:12,370 So let's take a look at how we're going to actually use this thing as a quick reminder. 4 00:00:12,410 --> 00:00:18,480 Right now our publisher T.S. file is publishing events on a channel or with a subject of ticket colon 5 00:00:18,530 --> 00:00:19,530 created. 6 00:00:19,790 --> 00:00:25,130 So let's make a listener kind of like this one hour here a ticket created listener to do so. 7 00:00:25,130 --> 00:00:30,140 We are going to extend the listener class and then just define those three abstract properties inside 8 00:00:30,140 --> 00:00:38,790 of it subject on message and group name tobacco over inside my editor at the very bottom I'll define 9 00:00:38,820 --> 00:00:47,990 a class called Ticket created listener and this is going to be extending the listener abstract class 10 00:00:48,590 --> 00:00:49,510 as soon as we write that out. 11 00:00:49,520 --> 00:00:50,630 We're gonna get some errors here. 12 00:00:50,630 --> 00:00:55,730 So again this is typescript telling us that if we want to extend listener we have to define on message 13 00:00:56,050 --> 00:01:02,240 Q Group Name and subject to remember using these abstract class things is really fantastic because typescript 14 00:01:02,240 --> 00:01:06,240 is going to help you implement parts of your code for you. 15 00:01:06,270 --> 00:01:10,620 Let's first take care of subject so this will be the name of the channel that we want to listen to in 16 00:01:10,620 --> 00:01:11,580 our case it is ticket. 17 00:01:11,580 --> 00:01:17,520 Colon created again because inside of our publisher that is the channel that we are publishing information 18 00:01:17,540 --> 00:01:19,660 to. 19 00:01:19,660 --> 00:01:22,090 Next up is our Q group name. 20 00:01:22,090 --> 00:01:26,920 Remember the only requirement here is that is some consistent value because we are essentially joining 21 00:01:26,920 --> 00:01:29,190 a Q group with other services. 22 00:01:29,410 --> 00:01:34,840 And whenever an event comes in the message will be randomly distributed to one of the instances or one 23 00:01:34,840 --> 00:01:37,320 of the listeners inside that Q Group. 24 00:01:37,320 --> 00:01:40,490 So let's imagine that we are creating this listener of ticket created. 25 00:01:40,530 --> 00:01:42,360 How about inside of our payment service. 26 00:01:42,370 --> 00:01:46,480 We've not yet put it together but we'll assume that we are writing out this listener inside there. 27 00:01:46,480 --> 00:01:54,350 So I'm going to try to join a Q Group Name of payments service and then finally we'll implement implement 28 00:01:54,440 --> 00:02:00,570 on message on message is going to receive our event data or the actual data coming out of the event. 29 00:02:00,570 --> 00:02:03,590 And it's also going to receive the event itself as the second argument. 30 00:02:04,250 --> 00:02:07,240 So put in on message 31 00:02:10,190 --> 00:02:15,560 this thing is going to receive our event data which is of type any for right now we're going to provide 32 00:02:15,590 --> 00:02:20,910 a better type of that in just a moment but right now he is totally fine and then the actual message 33 00:02:20,940 --> 00:02:26,770 as the second argument so then inside of here we can run whatever business logic we want to run. 34 00:02:26,760 --> 00:02:32,280 We can try to take the data handle in some fashion update something in our database or whatever we need 35 00:02:32,280 --> 00:02:33,210 to do. 36 00:02:33,540 --> 00:02:41,830 So right now I'll just do a console log and say event data and the console log out the data. 37 00:02:41,910 --> 00:02:46,890 Now one other quick thing inside of on message we're presumably going to have some business logic inside 38 00:02:46,890 --> 00:02:47,410 of here. 39 00:02:47,490 --> 00:02:52,950 And if the business logic fails for any reason remember we want to just allow this message to time out 40 00:02:53,220 --> 00:02:57,990 to fail deliver essentially so that Nat's attempts to re deliver it automatically at some point on the 41 00:02:57,990 --> 00:03:05,910 future so if everything goes correctly we are going to call message dot ACH and all its message. 42 00:03:06,000 --> 00:03:10,320 Otherwise if something goes poorly we are not going to act the message and just allow the message to 43 00:03:10,320 --> 00:03:10,840 time out. 44 00:03:11,400 --> 00:03:16,890 So right now we'll assume that everything inside of here goes correctly and we'll do a message dot EQ 45 00:03:16,980 --> 00:03:18,350 like so. 46 00:03:18,360 --> 00:03:24,520 So that is what's going to actually mark this message as successfully having been passed. 47 00:03:24,650 --> 00:03:27,080 So let's save this now to actually use it. 48 00:03:27,080 --> 00:03:30,160 We're going to need to create an instance of ticket create a listener. 49 00:03:30,620 --> 00:03:37,310 And remember this thing's constructor was defined inside of our listener and the constructor requires 50 00:03:37,310 --> 00:03:43,120 us to provide a pre initialized client so should be a client that is already connected to our net server 51 00:03:44,160 --> 00:03:48,390 where he went through that process of creating a client and connecting it up the top the file. 52 00:03:48,390 --> 00:03:49,690 So let's go out there right now. 53 00:03:51,490 --> 00:03:52,950 So this is where we create our clients. 54 00:03:52,960 --> 00:03:57,710 We then attempted to connect and we listen for the Connect event. 55 00:03:57,820 --> 00:04:01,690 We still want to keep around all the on closed stuff to make sure that we clean up our connection. 56 00:04:01,930 --> 00:04:06,190 But we do not need to create this option stuff because we have already gone ahead and implemented all 57 00:04:06,190 --> 00:04:10,070 this inside that listener class so I can delete that options block. 58 00:04:10,250 --> 00:04:15,770 I'm going to delete the subscription that we created and I'll delete where we actually handled messages 59 00:04:15,770 --> 00:04:18,140 inside a subscription. 60 00:04:18,150 --> 00:04:23,430 So now instead to do pretty much everything we were doing previously we should only have to call new 61 00:04:24,530 --> 00:04:34,450 ticket created listener and pass in our Nats client of Stan. 62 00:04:34,470 --> 00:04:38,040 Then after that we're going to make sure that we tell this thing to start listening for incoming data 63 00:04:38,520 --> 00:04:45,800 and we do so by calling that listen method so create the instance and call dot listen on it like so 64 00:04:47,560 --> 00:04:48,610 and that should be it. 65 00:04:48,620 --> 00:04:50,050 So let's save this. 66 00:04:50,180 --> 00:04:54,100 We're gonna flip back over to our terminal and see how we're doing. 67 00:04:54,150 --> 00:04:54,570 All right. 68 00:04:55,500 --> 00:04:58,620 So looks like everything was created. 69 00:04:58,620 --> 00:05:03,360 So we have received some message right here of ticket created on our payment service. 70 00:05:03,750 --> 00:05:11,310 And there's the event data from the on message method now in theory we should be able to also publish 71 00:05:11,310 --> 00:05:15,060 a new event and have it show up down here as well. 72 00:05:15,060 --> 00:05:16,400 Yep there it was right there. 73 00:05:16,410 --> 00:05:20,880 I know it's kinda hard to see if I clear that console to command okay and then republish I will see 74 00:05:20,880 --> 00:05:24,090 it appear in one of these two windows all right. 75 00:05:24,100 --> 00:05:29,830 Well I'd say that this is definitely a lot easier to read and eventually maybe not right now but eventually 76 00:05:29,830 --> 00:05:31,630 a lot easier to understand as well. 77 00:05:31,690 --> 00:05:36,880 So any time that you want to receive any events or somehow handle incoming events all we really do is 78 00:05:36,880 --> 00:05:38,480 implement a class like this right here. 79 00:05:38,590 --> 00:05:43,750 We'll list out the subject a queue group name and then define this on message and on message will eventually 80 00:05:43,750 --> 00:05:48,230 be called with some data and message itself. 81 00:05:48,330 --> 00:05:51,230 So this looks pretty good but still one or two other things I want to improve. 82 00:05:51,260 --> 00:05:53,000 So quick pause right here and I'll see you in just a minute.