1 00:00:01,830 --> 00:00:06,360 We've got our publisher put together and now it just needs an instance of a Nats client in order to 2 00:00:06,360 --> 00:00:07,900 successfully publish stuff. 3 00:00:08,070 --> 00:00:12,480 So we now need to decide where are we going to somehow create our client. 4 00:00:12,480 --> 00:00:16,880 Well I think that the best location to create this Nats client is going to be inside of our index s 5 00:00:17,010 --> 00:00:21,740 file if we open up our index out to yes file inside of our tickets s our see directory. 6 00:00:21,750 --> 00:00:26,760 You'll recall that inside of here we've already got some startup logic so we make sure that we say connect 7 00:00:26,760 --> 00:00:33,270 to our Mongo DB instead successfully through mongoose before we ever even start up our Express application 8 00:00:33,420 --> 00:00:39,430 or technically start listening to traffic just as our application really needs to have a connection 9 00:00:39,430 --> 00:00:41,750 to a mongo D.B. to work successfully. 10 00:00:41,770 --> 00:00:47,300 Our app also needs to have a connection to Nat streaming server in order for it to work successfully. 11 00:00:47,440 --> 00:00:51,910 So we should probably make sure that we somehow create our Nats client or connect to Nats inside of 12 00:00:51,910 --> 00:00:58,480 here before we ever start listening to traffic that starts to cause a little bit of an issue. 13 00:00:58,490 --> 00:01:01,700 Let me show you why OK. 14 00:01:01,740 --> 00:01:04,640 So this is a diagram of some different files we have inside of our project. 15 00:01:05,130 --> 00:01:10,250 So we're saying that inside of our index that TSA file we want to initialize a Nats client. 16 00:01:10,250 --> 00:01:14,880 Now when we think about creating this Nats client there's a very important distinction to make between 17 00:01:14,940 --> 00:01:20,070 the Nats client that we are using and Mongoose which we just said well we need to connect to. 18 00:01:20,160 --> 00:01:26,160 So here's our connection statement for Mongoose when we connect with Mongoose Mongoose internally keeps 19 00:01:26,160 --> 00:01:31,440 a record of all the different connections that exist so we can now after calling connect on this we 20 00:01:31,440 --> 00:01:36,270 can open up any other file inside of our project and say hey import mongoose. 21 00:01:36,270 --> 00:01:40,650 And today here is a copy of Mongoose that is already connected to some server. 22 00:01:40,880 --> 00:01:45,450 And so we could really make use of Mongoose or through our different models we create to reach out to 23 00:01:45,450 --> 00:01:47,750 mongo D.B. make queries and whatnot. 24 00:01:48,820 --> 00:01:55,150 However the node Nat streaming library that we are using to connect to Nats works in a very different 25 00:01:55,150 --> 00:01:56,030 fashion. 26 00:01:56,150 --> 00:02:01,330 I'm going to go back to my Nats test directory and take a look at the publisher inside the publisher 27 00:02:01,330 --> 00:02:02,210 T.S. file. 28 00:02:02,260 --> 00:02:08,140 You'll recall that when we connect with nets there is not some internal connection or internal object 29 00:02:08,440 --> 00:02:12,010 keeping track of this connection inside of this Nats library. 30 00:02:12,100 --> 00:02:19,290 Instead when we call Nats dot connect it directly returns to us the clients so this client right here 31 00:02:19,380 --> 00:02:24,840 is what we need to somehow manually share among all the different files inside of our project whereas 32 00:02:24,930 --> 00:02:30,030 with Mongoose we don't get any distinct client back and said Mongoose internally keeps track of this 33 00:02:30,030 --> 00:02:36,270 connection and everything related to it so in other words inside of our index not TSA file we're going 34 00:02:36,270 --> 00:02:42,210 to have a Nats client object and we want to somehow share that with a bunch of other files inside of 35 00:02:42,210 --> 00:02:43,210 our project. 36 00:02:43,440 --> 00:02:46,040 But that starts to raise a really big issue. 37 00:02:46,320 --> 00:02:52,590 Our ticket created root handler is being imported into the app yes file which in turn is getting imported 38 00:02:52,590 --> 00:02:54,480 into index not to us. 39 00:02:54,510 --> 00:02:57,800 So we've already got kind of a one way dependency right here. 40 00:02:57,900 --> 00:03:02,880 We are importing this file into that file into that file and we're now starting to say that we want 41 00:03:02,880 --> 00:03:08,130 to have some kind of Nats client being created and presumably we're going to want to export that from 42 00:03:08,130 --> 00:03:14,790 index dot to yes and then possibly imported into the ticket created root handler so notice the circle 43 00:03:14,790 --> 00:03:16,630 we're kind of making here of imports. 44 00:03:16,650 --> 00:03:22,580 We are importing this thing this way and then we are importing this thing this way We're essentially 45 00:03:22,580 --> 00:03:29,360 creating a cyclical import or a cyclical dependency here where this file requires this one and this 46 00:03:29,360 --> 00:03:31,020 file requires this one. 47 00:03:31,070 --> 00:03:35,450 That's something we can't technically do but it would be really fantastic if we could figure out some 48 00:03:35,450 --> 00:03:43,510 way to avoid creating this cyclical dependency so we want to still somehow initialize this Nats client 49 00:03:43,570 --> 00:03:49,360 inside of our index not t s file but we do not want to have it well need to be imported over to the 50 00:03:49,360 --> 00:03:53,670 ticket handler or ticket created root handler from index start to yes. 51 00:03:53,680 --> 00:03:55,800 So how we gonna solve this. 52 00:03:55,910 --> 00:03:57,310 Well here's what we're going to do. 53 00:03:57,470 --> 00:03:59,570 We're going to make a new file called Nats client. 54 00:03:59,590 --> 00:04:00,380 Yes. 55 00:04:00,500 --> 00:04:06,200 And the goal of this file is going to be to somehow create a Nats client and behave as a singleton of 56 00:04:06,200 --> 00:04:07,160 sorts. 57 00:04:07,160 --> 00:04:10,970 So we're going to try to build something very similar to Mongoose where we're going to have some kind 58 00:04:10,970 --> 00:04:15,530 of object that internally keeps track of some client and makes it available to everything else inside 59 00:04:15,530 --> 00:04:22,170 of our app the indexed yes file is then going to import that and make sure that we have some code inside 60 00:04:22,170 --> 00:04:26,100 there to actually initialize that Nats client and connect to that streaming server. 61 00:04:26,100 --> 00:04:30,660 We will also have some code inside there to make sure that if we ever lose our connection to Nats or 62 00:04:30,660 --> 00:04:36,270 if we are about to shut down our application we did go through some graceful shutdown process just as 63 00:04:36,270 --> 00:04:40,670 a reminder that entire graceful shutdown stuff was really important to do. 64 00:04:40,680 --> 00:04:46,410 If we take a look back at say our listener we really had to do that really graceful shutdown stuff because 65 00:04:46,440 --> 00:04:50,700 otherwise for some period of time the Nats streaming server might think that our client is still active 66 00:04:50,970 --> 00:04:56,450 which we really did not want so we're going to do the initialization of that thing inside of index not 67 00:04:56,450 --> 00:05:01,520 t us and then we'll also be able to import that initialized client or kind of pre initialized client 68 00:05:01,880 --> 00:05:06,950 into our ticket created root handler and make use of it inside the root handler itself. 69 00:05:06,950 --> 00:05:11,150 So long story short we need to create this extra file we need to have something inside there that's 70 00:05:11,150 --> 00:05:17,000 going to behave as a singleton of sorts and allow us to get access to this client inside of a bunch 71 00:05:17,000 --> 00:05:19,670 of different files yes. 72 00:05:19,680 --> 00:05:21,190 Now we've got a better idea what's going on. 73 00:05:21,190 --> 00:05:22,930 Let's start to implement this in just a moment.