We've got our publisher put together and now it just needs an instance of a Nats client in order to
successfully publish stuff.
So we now need to decide where are we going to somehow create our client.
Well I think that the best location to create this Nats client is going to be inside of our index s
file if we open up our index out to yes file inside of our tickets s our see directory.
You'll recall that inside of here we've already got some startup logic so we make sure that we say connect
to our Mongo DB instead successfully through mongoose before we ever even start up our Express application
or technically start listening to traffic just as our application really needs to have a connection
to a mongo D.B. to work successfully.
Our app also needs to have a connection to Nat streaming server in order for it to work successfully.
So we should probably make sure that we somehow create our Nats client or connect to Nats inside of
here before we ever start listening to traffic that starts to cause a little bit of an issue.
Let me show you why OK.
So this is a diagram of some different files we have inside of our project.
So we're saying that inside of our index that TSA file we want to initialize a Nats client.
Now when we think about creating this Nats client there's a very important distinction to make between
the Nats client that we are using and Mongoose which we just said well we need to connect to.
So here's our connection statement for Mongoose when we connect with Mongoose Mongoose internally keeps
a record of all the different connections that exist so we can now after calling connect on this we
can open up any other file inside of our project and say hey import mongoose.
And today here is a copy of Mongoose that is already connected to some server.
And so we could really make use of Mongoose or through our different models we create to reach out to
mongo D.B. make queries and whatnot.
However the node Nat streaming library that we are using to connect to Nats works in a very different
fashion.
I'm going to go back to my Nats test directory and take a look at the publisher inside the publisher
T.S. file.
You'll recall that when we connect with nets there is not some internal connection or internal object
keeping track of this connection inside of this Nats library.
Instead when we call Nats dot connect it directly returns to us the clients so this client right here
is what we need to somehow manually share among all the different files inside of our project whereas
with Mongoose we don't get any distinct client back and said Mongoose internally keeps track of this
connection and everything related to it so in other words inside of our index not TSA file we're going
to have a Nats client object and we want to somehow share that with a bunch of other files inside of
our project.
But that starts to raise a really big issue.
Our ticket created root handler is being imported into the app yes file which in turn is getting imported
into index not to us.
So we've already got kind of a one way dependency right here.
We are importing this file into that file into that file and we're now starting to say that we want
to have some kind of Nats client being created and presumably we're going to want to export that from
index dot to yes and then possibly imported into the ticket created root handler so notice the circle
we're kind of making here of imports.
We are importing this thing this way and then we are importing this thing this way We're essentially
creating a cyclical import or a cyclical dependency here where this file requires this one and this
file requires this one.
That's something we can't technically do but it would be really fantastic if we could figure out some
way to avoid creating this cyclical dependency so we want to still somehow initialize this Nats client
inside of our index not t s file but we do not want to have it well need to be imported over to the
ticket handler or ticket created root handler from index start to yes.
So how we gonna solve this.
Well here's what we're going to do.
We're going to make a new file called Nats client.
Yes.
And the goal of this file is going to be to somehow create a Nats client and behave as a singleton of
sorts.
So we're going to try to build something very similar to Mongoose where we're going to have some kind
of object that internally keeps track of some client and makes it available to everything else inside
of our app the indexed yes file is then going to import that and make sure that we have some code inside
there to actually initialize that Nats client and connect to that streaming server.
We will also have some code inside there to make sure that if we ever lose our connection to Nats or
if we are about to shut down our application we did go through some graceful shutdown process just as
a reminder that entire graceful shutdown stuff was really important to do.
If we take a look back at say our listener we really had to do that really graceful shutdown stuff because
otherwise for some period of time the Nats streaming server might think that our client is still active
which we really did not want so we're going to do the initialization of that thing inside of index not
t us and then we'll also be able to import that initialized client or kind of pre initialized client
into our ticket created root handler and make use of it inside the root handler itself.
So long story short we need to create this extra file we need to have something inside there that's
going to behave as a singleton of sorts and allow us to get access to this client inside of a bunch
of different files yes.
Now we've got a better idea what's going on.
Let's start to implement this in just a moment.