Let's get to work inside of our ticket service.
We're going to create a listener that is going to watch for that order created events and then any time
we see that event as I just mentioned we're going to lock that ticket down the back inside my editor
I'll find my tickets directory inside there I'll find the events folder and inside of events I'll make
a new directory of listeners and then inside that I'm going to make a listener file called order created
listener thought to yes.
Now I don't know about you but it's kind of hard to remember how to create a listener.
You might recall that we've gone through the process of creating a listener twice now and you might
recall that it's something around extending that base listener class that we had defined inside of our
common module.
But the actual details of it kind of easy to forget.
So here's how we would approach this using typescript to kind of help us through development at the
very minimum.
We probably want to remember that because we are trying to create a listener.
We're going to want to extend or subclass the listener base class we had put together that a very minimum.
Hopefully we can remember that we need to import the listener base class from our common module don't
import that and then I will export a new class called order read listener and that's going to extend
our listener base class.
Now if we can just remember this part right here typescript is going to help us the rest of the way.
So already we can see that typescript is saying hey this thing right here is a generic type and you
have to provide a type argument.
So we need to stick in as the generic type the event that we want to be listening for.
That's kind of our hint.
That's how we're gonna get hints along the way on how to actually implement this thing so from our common
module let's import the event that we want to listen for.
In this case it is as you guests by the name of our class right here the order created that
don't stick that in as the generic type and then after that typescript still has our back it's going
to tell us K Well if you want to do this here's what you have to do.
You have to implement that on message function a cougar name and a subject as well.
Well let's start off with the subject.
If you want to add a subject we probably need that subjects enum that we had previously declared inside
of our common module as well so from the com module I also get these subjects in the and then I'll define
the subject we want to listen for.
In this case I'm going to annotate the type of subject as subject not order created and then set its
value as subject start order created.
Next up we have to set the user group name.
Remember what we did back inside of our order service.
We created that separate file and we defined the Q Group name that was supposed to be used among all
of our different listeners.
Now it's just going to help us from or prevent us from making any silly typos.
So we'll take that same pattern once again inside of our listeners directory.
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
Name and because we are inside the ticket service organizing a Q Group Name of tickets service.
Simple enough going to save that then back inside of our listener at the very top.
I will import Q Group Name from same directory Q group name.
Now we can set that property on our class though.
Q Group Name is you group name and then finally our last step we have to implement the on message function
so we will define on message and remember this is where a touch script is not going to quite help us
out until we start to list out some arguments right here.
This thing is going to have to argue this provided the first is gonna be the data property of the event
though we're listening for and second it will be the message type from the no Nat streaming library
no put in here data and that's gonna be of type order created event specifically the data property of
that interface.
So order created event at data and then the second argument will be our message which is subtype message
again from the no Nat streaming library we definitely need to import that at the top as well.
No not streaming chances are on message function is going to have some async code inside of it.
So let's mark this boat this function as a sink as well.
So there we go again easy to forget what's going on inside of here.
But as long as we can just remember that we have to extend that base class that script is going to pretty
much help us along the way.
All right.
So we've got the listener put together.
Let's take a pause right here in the next video.
We're going to figure out some strategy some way of figuring out how to mark a ticket as being locked
down and prevent any changes from being made to it.