As you're just discussing inside of our order created listener right after we save our ticket we're
going to want to publish an event saying that this ticket was just updated that is going to allow our
different replicated services or services that have replicated data to stay in sync now to put this
update together.
I first want to give you a quick reminder on how we publish an event in general as we had spent a little
bit of time since we have published an event inside of our ticket service.
You might recall that we have a publishers directory inside there is the ticket updated publisher.
So this is the classic we used to publish an event saying that a ticket has been updated to make use
of this.
We create an instance of the publisher we pass in a Nats client.
So that might be something like Nats report clients and we'll call the publish function and is publish
function is going to have all the properties that are going to be contained inside the event.
Now unfortunately our ticket updated event definition is just a little bit out of date.
In particular you'll notice that we are saying that this event is going to have an I.D. a version title
price and user I.D. but our ticket also now has an order I.D. property tied to it as well this event
object right here does not yet support that order.
I.D. property chances are that whenever we update a ticket we're going to want to tell every other service
whether or not this ticket is reserved.
And we would do that by sharing that order I.D. property.
So we should probably go back over to our common module very quickly and make sure that we add in to
this event definition or the definition for ticket updated.
You say that the event is going to have an order I.D. property as well.
But before we do so there's one other very quick thing I want to mention I'm going to go back over to
the order created listener at the top.
I'm going to import the publisher that we're just taking a look at that we'll get the ticket updated.
Publisher from up one directory publishers ticket update publisher and then very quickly I want to imagine
how we're going to write this out right after that ticket not safe.
Well just as we saw on that other file we would probably write out something that looks like new ticket
updated publisher and then we would want to pass in our Nats client right here.
Now there are two different ways that we can provide a Nats client.
One is going to be very easy and direct the others can be a little bit harder to understand but ultimately
a lot easier.
So in order to provide a Nats client right here the easy solution that will work right away without
any changes would be to import our Nats rapper from up to directories Nats rapper and then we could
provide that to the publisher
like so easy enough.
No problem whatsoever.
Right.
Well there is a downside to this approach.
The downside is that we are adding in yet another tie between two different files inside of our projects.
So we are importing the Nats wrapper directly into order created listener.
Normally that is unavoidable at some point time.
We just have to tie different files together.
And that's life.
That's just how stuff works.
However because you have already seen that testing a lot of these listeners revolves around somehow
mocking this Nats rapper right here.
If we had in a direct report for NATS rapper that might make testing a little bit more challenging down
the line.
So it turns out that there actually is a slightly easier way to approach this.
It is just going to involve making one other very quick change inside of our common module.
So let's take a look at a quick diagram and understand what the other option is.
OK so this is what we are doing right now.
We've got our order created listener.
It is trying to create an instance of ticket updated publisher and we are doing so by importing the
Nats rapper into the order created a listener.
We're going to take that things client and then pass it off to the ticket.
Updated publisher but there's something else I want you to recall the order created listener is a subclass
of the listener base class.
Let's go take a look at that listener base class definition.
Very quickly we're going to see something really interesting our listener base class is defined inside
of our common module so I'll go to my common module s our RC events and then find base listener T.S.
inside of here.
You might recall that whenever we create an instance of a listener or anything that extends a listener
for that matter we are required to provide a Nats client.
That's what this constructor function is saying right here.
We then assign that to the distant client property and we had marked this stock client or the client
property as private which means that it cannot be accessed by any other class even a subclass so technically
our order created listener.
That is what we are working on right now.
It is a subclass or a child class of the listener.
So inside of order create a listener they're technically already is a Nats client kind of wrapped up
into this thing but unfortunately we had marked it as a private property which means that this child
class cannot access that client property diagram format.
This is what it looks like we've got our ordinary listener.
It is extending the listener listener definitely already has a initialized Nats client is there ready
to be taken.
So if we could somehow get a handle on that thing we could very easily pass it through to the ticket
update a publisher without having to worry about that Nats wrapper class or anything like that.
The only reason we cannot do this right now is because we had marked that client property as private
and again if a client or to if a property is marked as private we can not access it from anywhere outside
the class even in a child class.
So to fix this inside of our common module we could just update the modifier on that property.
We could instead market as protected if we market as protected.
That means that the child class of ordinary listener can access that property it can access the client
take the client and pass it off to the ticket.
Updated publisher and so this would be a way preferable solution because it's gonna make testing easier
down the line.
And it's also going to mean that we've got one less direct tie between two different files inside of
a project which again is usually a pretty good thing.
OK.
So at this point what we've really established inside of this video is that we really need to go over
to our common module and make two changes.
The first change we need to make is a change to our ticket updated events.
We need to make sure that the ticket update event also communicates an order ideas property and then
we've also established that our base listener.
We need to mark that client as a protected property instead of private now that we've established what
we have to do.
Quick pause right here.
Make those two changes in the next video.