1 00:00:01,890 --> 00:00:03,950 Let's get to work inside of our ticket service. 2 00:00:03,960 --> 00:00:08,670 We're going to create a listener that is going to watch for that order created events and then any time 3 00:00:08,670 --> 00:00:13,290 we see that event as I just mentioned we're going to lock that ticket down the back inside my editor 4 00:00:14,450 --> 00:00:19,820 I'll find my tickets directory inside there I'll find the events folder and inside of events I'll make 5 00:00:19,820 --> 00:00:27,080 a new directory of listeners and then inside that I'm going to make a listener file called order created 6 00:00:27,440 --> 00:00:30,240 listener thought to yes. 7 00:00:30,350 --> 00:00:35,080 Now I don't know about you but it's kind of hard to remember how to create a listener. 8 00:00:35,270 --> 00:00:38,930 You might recall that we've gone through the process of creating a listener twice now and you might 9 00:00:38,930 --> 00:00:43,220 recall that it's something around extending that base listener class that we had defined inside of our 10 00:00:43,220 --> 00:00:44,360 common module. 11 00:00:44,360 --> 00:00:48,110 But the actual details of it kind of easy to forget. 12 00:00:48,120 --> 00:00:52,910 So here's how we would approach this using typescript to kind of help us through development at the 13 00:00:52,910 --> 00:00:53,590 very minimum. 14 00:00:53,620 --> 00:00:56,520 We probably want to remember that because we are trying to create a listener. 15 00:00:56,570 --> 00:01:02,300 We're going to want to extend or subclass the listener base class we had put together that a very minimum. 16 00:01:02,300 --> 00:01:11,940 Hopefully we can remember that we need to import the listener base class from our common module don't 17 00:01:11,970 --> 00:01:20,670 import that and then I will export a new class called order read listener and that's going to extend 18 00:01:20,760 --> 00:01:22,210 our listener base class. 19 00:01:22,230 --> 00:01:27,100 Now if we can just remember this part right here typescript is going to help us the rest of the way. 20 00:01:27,150 --> 00:01:32,100 So already we can see that typescript is saying hey this thing right here is a generic type and you 21 00:01:32,100 --> 00:01:34,190 have to provide a type argument. 22 00:01:34,350 --> 00:01:39,130 So we need to stick in as the generic type the event that we want to be listening for. 23 00:01:39,190 --> 00:01:40,020 That's kind of our hint. 24 00:01:40,020 --> 00:01:45,730 That's how we're gonna get hints along the way on how to actually implement this thing so from our common 25 00:01:45,730 --> 00:01:48,840 module let's import the event that we want to listen for. 26 00:01:48,880 --> 00:01:53,680 In this case it is as you guests by the name of our class right here the order created that 27 00:01:57,350 --> 00:02:03,990 don't stick that in as the generic type and then after that typescript still has our back it's going 28 00:02:03,990 --> 00:02:07,060 to tell us K Well if you want to do this here's what you have to do. 29 00:02:07,060 --> 00:02:12,250 You have to implement that on message function a cougar name and a subject as well. 30 00:02:12,240 --> 00:02:13,480 Well let's start off with the subject. 31 00:02:13,510 --> 00:02:19,210 If you want to add a subject we probably need that subjects enum that we had previously declared inside 32 00:02:19,210 --> 00:02:26,020 of our common module as well so from the com module I also get these subjects in the and then I'll define 33 00:02:26,020 --> 00:02:27,840 the subject we want to listen for. 34 00:02:27,970 --> 00:02:33,820 In this case I'm going to annotate the type of subject as subject not order created and then set its 35 00:02:33,820 --> 00:02:38,530 value as subject start order created. 36 00:02:38,540 --> 00:02:41,540 Next up we have to set the user group name. 37 00:02:41,540 --> 00:02:43,990 Remember what we did back inside of our order service. 38 00:02:44,000 --> 00:02:48,230 We created that separate file and we defined the Q Group name that was supposed to be used among all 39 00:02:48,230 --> 00:02:49,530 of our different listeners. 40 00:02:49,530 --> 00:02:53,540 Now it's just going to help us from or prevent us from making any silly typos. 41 00:02:53,690 --> 00:02:57,130 So we'll take that same pattern once again inside of our listeners directory. 42 00:02:57,140 --> 00:03:06,610 We'll make a new file of Q Group Name that T.S. and inside of here I will export a concept of Q Group 43 00:03:06,880 --> 00:03:13,280 Name and because we are inside the ticket service organizing a Q Group Name of tickets service. 44 00:03:13,280 --> 00:03:19,540 Simple enough going to save that then back inside of our listener at the very top. 45 00:03:19,570 --> 00:03:26,980 I will import Q Group Name from same directory Q group name. 46 00:03:27,070 --> 00:03:29,940 Now we can set that property on our class though. 47 00:03:29,950 --> 00:03:39,450 Q Group Name is you group name and then finally our last step we have to implement the on message function 48 00:03:40,890 --> 00:03:46,370 so we will define on message and remember this is where a touch script is not going to quite help us 49 00:03:46,370 --> 00:03:48,740 out until we start to list out some arguments right here. 50 00:03:48,740 --> 00:03:53,840 This thing is going to have to argue this provided the first is gonna be the data property of the event 51 00:03:53,840 --> 00:03:59,360 though we're listening for and second it will be the message type from the no Nat streaming library 52 00:04:00,020 --> 00:04:05,960 no put in here data and that's gonna be of type order created event specifically the data property of 53 00:04:05,960 --> 00:04:07,310 that interface. 54 00:04:07,310 --> 00:04:15,670 So order created event at data and then the second argument will be our message which is subtype message 55 00:04:15,680 --> 00:04:25,060 again from the no Nat streaming library we definitely need to import that at the top as well. 56 00:04:25,280 --> 00:04:31,240 No not streaming chances are on message function is going to have some async code inside of it. 57 00:04:31,240 --> 00:04:35,580 So let's mark this boat this function as a sink as well. 58 00:04:35,640 --> 00:04:39,720 So there we go again easy to forget what's going on inside of here. 59 00:04:39,780 --> 00:04:43,830 But as long as we can just remember that we have to extend that base class that script is going to pretty 60 00:04:43,830 --> 00:04:45,080 much help us along the way. 61 00:04:46,480 --> 00:04:46,780 All right. 62 00:04:46,810 --> 00:04:48,910 So we've got the listener put together. 63 00:04:48,910 --> 00:04:50,470 Let's take a pause right here in the next video. 64 00:04:50,470 --> 00:04:55,960 We're going to figure out some strategy some way of figuring out how to mark a ticket as being locked 65 00:04:55,970 --> 00:04:58,450 down and prevent any changes from being made to it.