All right.
Let's continue working on the implementation of this Nat's wrapper class.
So inside of here we want to eventually create a Nats client.
In other words the client that we get back from the Nats library itself.
We're going to eventually create that and assign it as a property it to our class.
So I'm going to add in a private property called client and its type is going to stand and notice some
calling this thing underscore a client.
You'll see why for that or why we're doing that in just a moment.
Now we are not going to try to define this property inside of any constructor or anything like that
because we are running our constructor right here.
And so at this point time it is too early to try to create a actual Nats client and assign it to this
client property instead.
We do not want to create and assign this client property until we eventually tried to call it connect
or something like that from inside of our index start to yes file touch.
However wants to make sure that we tried to either immediately initialize this property on the same
line of code or inside the constructor and that's why we're seeing an error right here.
So to get this air to go away we're gonna say underscore client and then question mark that tells typescript
that this property might be undefined for some periods of time.
After that we're then going to create a function called next.
So again very similar to mongoose.
We're going to eventually call this connect function with our different connection settings from inside
of index dot yes inside of connect.
We're going to create an actual instance of the Nats client and assign it to this underscore client
variable to connect very similar to.
Once again we're going to assume that this thing is going to receive some connection settings.
So for example the cluster idea that we want to connect to the client that we want to name ourselves
as and the URL to try to connect you as well.
Essentially the same connection settings you might recall having written back inside of it Nats test
project inside that listener A.S. file.
So this is what you refer to as our cluster I.D..
I'll show you where that comes from in just a moment.
We spoke about the client I.D. and then the URL that we actually want to connect to.
So we're going to receive all those three things as arguments to this connect function so say cluster
I.T. going to be a string fine idea will be a string and you l will be a string as well.
Then inside of here we're going to assign this dot underscore client and that's going to come from calling
Nats connect.
We're gonna pass in a cluster I.D. line I.D. and the URL inside of an object.
Make sure you sure you URL is inside of an object like some then normally to somehow get a signal that
we have connected we would write out something like client dot on connect and then put in a callback
function and that callback function would be invoked once Nats actually successfully connects to our
NAT server and so inside that callback function we might do something like a console log of connected
to Nats.
Now this callback semantics right here not that great in today's day and age right.
It'll be a lot better if we could call connect and then be able to use the same kind of async await
syntax as we were able to use with Mongoose connect.
So really we want to somehow take this callback function right here and twist it turn it allow us to
use the async await syntax instead you might recall that we already did something very similar to this
a little bit ago we essentially took the callback function and wrapped it with a promise and then we
manually resolved that promise from inside the callback.
So let's do that same thing right here.
So from connect I'm going to return a new promise
I'm going to put in the resolve and reject all then move in the this dot underscore a client inside
their and then in the success case right after doing that console log I will resolve the promise you'll
notice we have an air around this dot underscore a client we'll just ignore that for a moment now if
we're doing everything correctly we should probably also make sure we handle the heir case the case
in which we fail to connect and that's going to also add in a handler to watch for where we fail to
connect but we're going to do a this dot underscore our clients on air and then put a callback function.
This is how we handle a connection error during the initial connection attempt.
If we fail to connect this callback function will be invoked and the first argument will be some error
describing exactly what went wrong.
So in this case we're going to reject our promise and we will reject it with that error.
All right.
So why is typescript complaining right here if we hover over that thing it says object is possibly undefined.
The reason for this is that typescript understands that we just assigned the client property right here.
So at this one time if we write out this dot underscore client typescript says oh OK sure.
Like Klein exists you just defined it two seconds ago but because we are now trying to access this dot
underscore a client inside of a callback function right here typescript is thinking that we could have
accidentally reassigned or unassigned really the this dot underscore a client between this line of code
and this line of code.
Now that is extremely unlikely to happen but it is what typescript is designed to do.
It is meant to capture errors where a variable might suddenly flip back to going to undefined.
We're going to put in a very temporary fix for this that we're going to change in just a little bit
but I really want to test out the code we put together so far to just get this aired to go away.
We're gonna put a exclamation right after this dot underscore a client on both those okay.
So let's say this.
And I would now like to do a quick test and just make sure that we can actually connect to Nats.
We want to be able to call this connect function and use the async await syntax on it so to do so we'll
go back over to index dot s at the very top.
I'm going to import Nats rapper from NATS rapper.
Notice I'm using a lowercase and right here.
The reason for that is that we are indicating that this is an instance of the class and that's rapper.
We only have one instance floating around the entire application because we are not exporting the class.
We are exporting one single instance that will be shared between all of our different files so down
here right before our Mongoose dot connect.
I'm going to put in and await Nats rapper not connect and we'll put in those three connection settings
the first one is going to be our cluster I.D. and I told you that I would tell you where that is coming
from.
So let's do that really quickly if we think back to the Nats test project and open up that listener
you'll recall that for that cluster I.D. or the first argument we put in a string of ticketing.
So why is that.
Well if we think back also to our infra directory inside there we had the Nats deployment file so here's
the Nats deployment file.
Here's our pod spec and here's the arguments we fed to that container one of the arguments we provided
was see I.D. and we give it a value of ticketing.
See I.D. stands for a cluster I.D..
This is going to assign an I.D. to the cluster in our case.
The idea simply ticketing that's what the first argument is we're specifying the idea of the cluster
that we're trying to connect to.
It's open and ticketing or that first arguments then the second argument you'll recall is going to be
our client I.D. We need to make sure that this is a more or less random value.
Right now we've just put in some kind of random string.
And then finally we have to put into your all that we want to connect to ask the third arguments the
world that we going to connect to is going to be at the service that is governing access to our Nats
deployments.
Remember we defined that service at the bottom of this file its name was Nats SRB and we specifically
want to connect to port for 2 2 2 so for the URL at the very end of connect right here we will put in
H GDP calling slash slash Nats SRB colon 4 2 2 2 OK.
So that should be enough to at least test this out and make sure that we can actually connect to it.
That's going to save this.
And then going to go back over to my scaffold window which I'm still running at my terminal and I'll
see it connected to nets.
Now if you failed to connect for any reason I would really encourage you to take a look at that message.
Also if you don't see anything around that's down here try stopping scaffold and restarting it.
Now if you see an error I would suspect that you might have a duplicate client idea news.
So try changing the client idea to something else saving it and that should get you your automatic refresh
and then you should see connected to Nats if you still do not have managed to have any success.
Don't sweat it.
HOPPER The a post your code on github link me to your code and I'll do my best to help you debug this.
All right.
So it looks like we were able to connect to Nats and most importantly we were able to do so using some
semantics that look very similar to Mongoose.
So this is definitely good.
But now I'm kind of curious how are we going to actually use this thing from inside of say our new root
handler where we are creating a ticket.
So still a little bit of work to do.
Let's take a pause right here and return in just a moment.