The publisher is all put together so let's now work on implementation for our listener.
The goal of our listener.
We're going to come up with a subject name or the channel that we want to listen to.
We're gonna pass that into the stand client to create a subscription and then we're going to watch this
subscription or anytime it receives some data to back inside of my listener T.S. file.
At the very top we're going to import Nats once again from node that streaming will then create my clients
which remember we referred to Muni convention as simply Stan so that's dot connect to once again put
in some kind of mysterious looking arguments right here.
Just bear with me for a moment we will come back to the Connect function.
It's going to put in ticketing.
I'm going to put in one two three and then an object and inside there will be a your l and very similar
to what we had last time we'll put an H TTP local host or at 4 2 2 2
0 then watch for a Connect event
so after we have successfully connected once again let's just do a quick console log and say hey our
listener connected to Nats I'll do a console log of listener connected to Nats let's now go over to
our terminal and just tried to run this program and make sure that the listener can connect as well.
So back at my terminal I'm going to find a window where I was running the publisher.
If you are running a terminal where you can do a split screen I highly recommend you tried to pull up
a split screen right now.
So for me I'm making use of item two on Mac OS a hotkey to do that is command D so I can hit command
D.
And now I get the split screen right here so this will allow us to watch events being published and
listen for at the same time.
If you do not have a terminal where you can do a split screen no problem just open up a second terminal
window and maybe drag them next to each other just like this.
You can see the events being published and listened to at the same time then inside this second window.
I'm still inside of my Nats dash test directory and inside there I will run NPM run listener to me NPM
run listen we gave it a slightly different script name so NPM run listen and it looks like all right.
We were able to connect to Nats at this point.
We've got a lot of extra information inside of here so this restart stuff typescript log in information
and so on.
So really quickly I can go back to both these scripts and right after the import statements I'm going
to add in a console dot clear I'm gonna put them inside of listener and I'm going to put it inside of
the publisher as well.
Right after the import statement now whenever this stuff runs or whenever our code gets reran because
we change the file now we'll just see the console logs at the very top and just remove a lot of the
garbage that was in here previously one other thing I wanna mention very quickly and this is gonna become
very relevant as we're gonna want to start to publish a lot of events here in just a moment.
If we ever want to restart our program while we are using T.S. no Dev we can select the terminal window
that's running our program type in the letters R S and then hit enter and that will automatically restart
our program.
So right now every single time I type in RSS and then hit enter I am restarting my program which means
I'd create a new connection to the Net server and then I publish a brand new event.
So if I do this a couple more times I've now probably published like five six or seven different events
on that particular channel the channel with the subject of ticket colon created OK.
So let's now go back over and work on our listener implementation inside of listener.
Yes.
After we successfully connect we're going to come up with the name of the subject that we want to listen
to and use it to create a subscription so I'm going to write right after the council log on to subscription
is Stan dot subscribe and we're gonna put in our subject right here which is ticket colon created then
after creating the subscription.
Remember this is the object that we're going to listen to and we're going to receive some data through
the subscription contrary to how many other libraries work where you might be used to adding in a callback
function like that that would be invoked with whatever data we receive not how it works with no dates
dreaming.
Instead we create the subscription first and then listen to a very particular type of event off the
subscription itself.
We're going to call subscription dot on message so as I mentioned at the end of last video in the world
of Nats they don't really refer to these events being exchanged as events.
Instead they referred to as messages you'll see this message.
Terminology floating around a little bit then the argument to this is going to be the actual message
which we frequently abbreviate as simply NSG.
It must be right here is not just the raw data.
Instead it's got a couple of different properties inside of it.
We'll take a look at over time so right now let's just do a console log of.
Message received so we're not actually looking at that message.
We're not actually trying to inspect any of the data inside it just yet.
I just want to make sure that we can actually receive a message that's all OK.
I'm going to save this.
I can make sure both files are saved.
And then I'll go back over to my terminal.
Now in theory our listener is creating a subscription and listening to that very specific channel.
So let's now go back over to our publisher.
We're going to restart the publisher and attempt to publish another event.
The instant we do so and I really do mean the very instant we do so we should see a console log from
the listener saying that it received a message.
So let's try this out on the publisher.
I'm gonna put an R S hit enter and there we go.
We have received that message.
OK.
So that's a very basic chain of publishing some data and receiving some data as well now.
Everything at this point really seems pretty simple and straightforward.
Right.
It's pretty much a basic eventing setup.
Nothing too crazy.
You might you think at this point that I'm going really slowly and he might be screaming at the screen
saying Steven can you like hurry it up and get to the point well believe you me we were going slow for
a reason it's really important for you to understand these very basic concepts because in the coming
videos we are going to absolutely blow the lid on complexity here.
We are really going to start to plumb the depths of gnats and really understand what is going on behind
the scenes because as you will see very very quickly some very small settings inside of here is going
to dramatically change how our application works.
So I'm really excited for the coming videos.
So quick browser here and we'll continue in just a moment.