We just put together our custom publisher before we test this out.
Once you know that I made a little typo in the last video you're going to go back over to the base publisher
right now we are receiving this data property right here.
We're taking that data and we are passing it directly off to publish.
But remember when we called publish on the Nats client we must be providing a string right here.
We're gonna take whatever data is provided and turn it into Jason.
We'll do a Jason string if I like so that was my mistake.
I just left off that string ify function call.
Now we've got that in there.
We're going to figure out how to actually run this code.
Well we're gonna go back over to our publisher test file inside of our SRT directory inside of here.
We're going to import the ticket created publisher that we just put together.
So that's this thing right here.
And we're going to use this ticket create a publisher to publish an event at the very top I'm going
to import ticket created publisher
from events ticket created publisher and we go that downside of our connect statement right after we
connect.
I'm going to comment out the data variable and the publisher call that's already in there.
I'm only commenting that out so we can do a comparison of how hard this code is to understand to be
for and after the refactor so we're then going to create an instance of that publisher.
So we'll say publisher is new ticket created publisher.
We have to pass in a Nats client.
So that's gonna be our stand variable that we had already declared and connected to Nats with and then
to actually publish something we'll call a publisher publish and we'll pass on the data that we want
to publish or this ticket created events as soon as I put in that empty object.
Notice how typescript starts to show some errors.
So typescript is doing some type checking for us it's making sure that whatever data we are trying to
publish has the correct properties.
So I.D. title and price and it's going to make sure that they have the correct types as well.
So must be a string a string and a number this check right here is what it is all about.
This is why we're going through all this extra effort to make sure that we do not try to publish some
incorrect or invalid data that's then going to be consumed by other services so we'll put in a hard
coded I.D. title of concert and a price of 20.
And as soon as I have the correct properties and the correct types it goes away.
And that means that we have defined this event successfully got time to test this out.
I'm going to look at my editor make sure I don't have any typescript errors from any files all day to
make sure I save all these different files.
I'll go back over to our terminal and do a quick test.
Now when you go back over to your terminal I would encourage you to just do a quick restart of the publisher
and the two listeners just in case there were some air along the way that caused the connection we messed
up or something like that so I'm going to restart my listener I can restart my second listener.
It looks like those both booted up correctly.
So if you get any errors chances are there's something wrong with the implementation right now I'll
then go to the window that was doing the publisher and do a NPM run publish so as soon as I start that
up I'll see the console log of 10 published I then see from one of the listeners that we received some
event data and I got the console log in there of the event data itself and then the I.D. the title and
the price awesome.
So it looks like the stuff is all working OK.
So this is definitely a fantastic improvement and I really cannot understate how important this refactor
was even if it was a little bit painful and at times rather confusing there's still a couple of things
I wanna show you really quickly.
Couple locations where we can fix up some code and make it a little bit easier to use inside of some
of our services in the future so quick pause right here and I'll see you in just a minute.