In the last video we spoke about the need to have some outside file called something like Nats client
or something similar.
And we should be creating some kind of client inside there and then initializing that client from index
stock to us.
The benefit to this approach is that we can then import that client directly into say our ticket created
row handler and not worry about any circular dependencies or anything like that before we start to write
out any implementation.
Putting this thing together.
This Nats client file is gonna be just a little bit confusing.
I want to give you a quick analogy here.
Something to just keep in mind as we were putting this file together that's going to help you really
understand what is going on behind the scenes back inside of our index not TSA file right now.
I want to once again point out Mongoose right here.
Take a look and consider how we use mongoose.
We import Mongoose into our index dot TSA file.
We then call connect on it and we pass in some connection settings.
We can then await for that connection to be completed.
After running this code right here.
Any other file inside of our project can import Mongoose and it will get a already reconnected pre initialized
version of Mongoose as well.
So for example our route handler right here the new route handler to create a ticket.
This thing could import Mongoose at the top.
It doesn't.
We use Mongoose technically through the ticket model but we could technically import Mongoose and use
it to reach directly out to our database without having to worry about initializing any connection or
anything like that.
We want to do something very similar with this Nats client that we are putting together.
So here's what's going on behind the scenes inside of mongoose.
We are importing Mongoose when we import Mongoose defined internally.
There's probably some class called Mongoose or something like that but you and I never actually work
with any kind of class called Mongoose instead.
We just import Mongoose and tada we get an instance of the Mongoose library or in this case the Mongoose
class so we can import that into indexed to us and inside there we can tell Mongoose to connect to a
very specific database.
We can await that connection to be established and so on then we can go to any other file inside of
our project we can import Mongoose and we get the same object like the same instance of the Mongoose
class so that same object is being shared between indexed not t s and our route handler.
So we want to do something identical with all of this Nats client stuff as well so we're gonna take
a very similar approach.
We're going to create inside this Nats client file that we're going to make a class called Nats wrapper.
The sole goal of this Nats wrapper class is to create and initialize a client from the Stan or the Nats
library.
No not streaming then rather than exporting the entire class.
We're going to create an instance of that class inside this file and export that instance.
So we will then be able to access the same object the same instance inside of indexed not to us and
inside of the ticket created root handler.
So just like Mongoose we can initialize or we will be able to initialize this client from inside of
index not t us and then by the time our ticket created row handler accesses or imports the thing the
client will have already been initialized so as we start to write this code I really just want you to
keep in mind how Mongoose works we are not saying hey create a new instance of a mongoose class or something
like that we just import Mongoose we call connect on it and it seems like everything just works.
Just keep in mind because we want to have an interface that is very similar to mongoose.
OK so with all that having been said let's start to create this Nats client file.
We're actually gonna end up calling this something different we're gonna call it Nats.
Not to us just to stay consistent with the class name.
We're probably gonna end up renaming both these things.
The only reason we're calling it Nats rapper right now is to make its purpose just really clear and
obvious for right now as we are writing this code.
All right.
So back over inside my SRT directory in the ticket's folder.
I'm gonna make a new file called Nats wrapper dot T.S. so inside of here we're going to import Nats
and the Stan type from node Nats streaming
then inside of here will create a class called Nats wrapper and then at the very bottom right away I'm
going to export a single instance of the thing so export concentrates wrapper is new Nats wrapper so
now we can create a client inside of here.
We can assign it as a property to the class and then once we create the instance out of that class we
are gonna share the instance around to all of our different files and we'll be able to say initialize
a client inside there from inside of index to yes and access that instance from inside the ticket created
root handler.
So again just keep mongoose in mind okay.
Before we start to work on the implementation I don't want this video to run too long.
So let's take a quick pause right here.