We've now got our abstract class listener which is going to make it a lot easier to create listeners
for all the different kinds of events that are going to be flowing around our application.
So let's take a look at how we're going to actually use this thing as a quick reminder.
Right now our publisher T.S. file is publishing events on a channel or with a subject of ticket colon
created.
So let's make a listener kind of like this one hour here a ticket created listener to do so.
We are going to extend the listener class and then just define those three abstract properties inside
of it subject on message and group name tobacco over inside my editor at the very bottom I'll define
a class called Ticket created listener and this is going to be extending the listener abstract class
as soon as we write that out.
We're gonna get some errors here.
So again this is typescript telling us that if we want to extend listener we have to define on message
Q Group Name and subject to remember using these abstract class things is really fantastic because typescript
is going to help you implement parts of your code for you.
Let's first take care of subject so this will be the name of the channel that we want to listen to in
our case it is ticket.
Colon created again because inside of our publisher that is the channel that we are publishing information
to.
Next up is our Q group name.
Remember the only requirement here is that is some consistent value because we are essentially joining
a Q group with other services.
And whenever an event comes in the message will be randomly distributed to one of the instances or one
of the listeners inside that Q Group.
So let's imagine that we are creating this listener of ticket created.
How about inside of our payment service.
We've not yet put it together but we'll assume that we are writing out this listener inside there.
So I'm going to try to join a Q Group Name of payments service and then finally we'll implement implement
on message on message is going to receive our event data or the actual data coming out of the event.
And it's also going to receive the event itself as the second argument.
So put in on message
this thing is going to receive our event data which is of type any for right now we're going to provide
a better type of that in just a moment but right now he is totally fine and then the actual message
as the second argument so then inside of here we can run whatever business logic we want to run.
We can try to take the data handle in some fashion update something in our database or whatever we need
to do.
So right now I'll just do a console log and say event data and the console log out the data.
Now one other quick thing inside of on message we're presumably going to have some business logic inside
of here.
And if the business logic fails for any reason remember we want to just allow this message to time out
to fail deliver essentially so that Nat's attempts to re deliver it automatically at some point on the
future so if everything goes correctly we are going to call message dot ACH and all its message.
Otherwise if something goes poorly we are not going to act the message and just allow the message to
time out.
So right now we'll assume that everything inside of here goes correctly and we'll do a message dot EQ
like so.
So that is what's going to actually mark this message as successfully having been passed.
So let's save this now to actually use it.
We're going to need to create an instance of ticket create a listener.
And remember this thing's constructor was defined inside of our listener and the constructor requires
us to provide a pre initialized client so should be a client that is already connected to our net server
where he went through that process of creating a client and connecting it up the top the file.
So let's go out there right now.
So this is where we create our clients.
We then attempted to connect and we listen for the Connect event.
We still want to keep around all the on closed stuff to make sure that we clean up our connection.
But we do not need to create this option stuff because we have already gone ahead and implemented all
this inside that listener class so I can delete that options block.
I'm going to delete the subscription that we created and I'll delete where we actually handled messages
inside a subscription.
So now instead to do pretty much everything we were doing previously we should only have to call new
ticket created listener and pass in our Nats client of Stan.
Then after that we're going to make sure that we tell this thing to start listening for incoming data
and we do so by calling that listen method so create the instance and call dot listen on it like so
and that should be it.
So let's save this.
We're gonna flip back over to our terminal and see how we're doing.
All right.
So looks like everything was created.
So we have received some message right here of ticket created on our payment service.
And there's the event data from the on message method now in theory we should be able to also publish
a new event and have it show up down here as well.
Yep there it was right there.
I know it's kinda hard to see if I clear that console to command okay and then republish I will see
it appear in one of these two windows all right.
Well I'd say that this is definitely a lot easier to read and eventually maybe not right now but eventually
a lot easier to understand as well.
So any time that you want to receive any events or somehow handle incoming events all we really do is
implement a class like this right here.
We'll list out the subject a queue group name and then define this on message and on message will eventually
be called with some data and message itself.
So this looks pretty good but still one or two other things I want to improve.
So quick pause right here and I'll see you in just a minute.