1 00:00:01,070 --> 00:00:05,820 In the last video we spoke about the need to have some outside file called something like Nats client 2 00:00:05,880 --> 00:00:07,200 or something similar. 3 00:00:07,200 --> 00:00:11,940 And we should be creating some kind of client inside there and then initializing that client from index 4 00:00:11,940 --> 00:00:13,210 stock to us. 5 00:00:13,230 --> 00:00:17,760 The benefit to this approach is that we can then import that client directly into say our ticket created 6 00:00:17,760 --> 00:00:23,160 row handler and not worry about any circular dependencies or anything like that before we start to write 7 00:00:23,160 --> 00:00:24,990 out any implementation. 8 00:00:24,990 --> 00:00:26,120 Putting this thing together. 9 00:00:26,130 --> 00:00:30,970 This Nats client file is gonna be just a little bit confusing. 10 00:00:31,070 --> 00:00:32,930 I want to give you a quick analogy here. 11 00:00:32,930 --> 00:00:37,010 Something to just keep in mind as we were putting this file together that's going to help you really 12 00:00:37,010 --> 00:00:41,530 understand what is going on behind the scenes back inside of our index not TSA file right now. 13 00:00:41,570 --> 00:00:44,750 I want to once again point out Mongoose right here. 14 00:00:44,810 --> 00:00:48,030 Take a look and consider how we use mongoose. 15 00:00:48,080 --> 00:00:51,790 We import Mongoose into our index dot TSA file. 16 00:00:51,880 --> 00:00:55,990 We then call connect on it and we pass in some connection settings. 17 00:00:55,990 --> 00:00:59,020 We can then await for that connection to be completed. 18 00:00:59,350 --> 00:01:01,150 After running this code right here. 19 00:01:01,150 --> 00:01:07,930 Any other file inside of our project can import Mongoose and it will get a already reconnected pre initialized 20 00:01:07,930 --> 00:01:10,150 version of Mongoose as well. 21 00:01:10,270 --> 00:01:15,160 So for example our route handler right here the new route handler to create a ticket. 22 00:01:15,190 --> 00:01:17,440 This thing could import Mongoose at the top. 23 00:01:17,440 --> 00:01:17,930 It doesn't. 24 00:01:17,930 --> 00:01:23,560 We use Mongoose technically through the ticket model but we could technically import Mongoose and use 25 00:01:23,560 --> 00:01:28,600 it to reach directly out to our database without having to worry about initializing any connection or 26 00:01:28,600 --> 00:01:31,010 anything like that. 27 00:01:31,110 --> 00:01:35,760 We want to do something very similar with this Nats client that we are putting together. 28 00:01:35,760 --> 00:01:39,750 So here's what's going on behind the scenes inside of mongoose. 29 00:01:39,870 --> 00:01:44,010 We are importing Mongoose when we import Mongoose defined internally. 30 00:01:44,070 --> 00:01:49,530 There's probably some class called Mongoose or something like that but you and I never actually work 31 00:01:49,530 --> 00:01:51,600 with any kind of class called Mongoose instead. 32 00:01:51,720 --> 00:01:56,970 We just import Mongoose and tada we get an instance of the Mongoose library or in this case the Mongoose 33 00:01:56,970 --> 00:02:02,730 class so we can import that into indexed to us and inside there we can tell Mongoose to connect to a 34 00:02:02,730 --> 00:02:04,250 very specific database. 35 00:02:04,320 --> 00:02:10,020 We can await that connection to be established and so on then we can go to any other file inside of 36 00:02:10,020 --> 00:02:16,410 our project we can import Mongoose and we get the same object like the same instance of the Mongoose 37 00:02:16,410 --> 00:02:22,670 class so that same object is being shared between indexed not t s and our route handler. 38 00:02:22,800 --> 00:02:27,930 So we want to do something identical with all of this Nats client stuff as well so we're gonna take 39 00:02:27,960 --> 00:02:29,910 a very similar approach. 40 00:02:30,000 --> 00:02:35,520 We're going to create inside this Nats client file that we're going to make a class called Nats wrapper. 41 00:02:35,640 --> 00:02:41,850 The sole goal of this Nats wrapper class is to create and initialize a client from the Stan or the Nats 42 00:02:41,850 --> 00:02:42,330 library. 43 00:02:42,330 --> 00:02:47,120 No not streaming then rather than exporting the entire class. 44 00:02:47,250 --> 00:02:51,850 We're going to create an instance of that class inside this file and export that instance. 45 00:02:52,020 --> 00:02:57,420 So we will then be able to access the same object the same instance inside of indexed not to us and 46 00:02:57,420 --> 00:03:00,150 inside of the ticket created root handler. 47 00:03:00,270 --> 00:03:05,640 So just like Mongoose we can initialize or we will be able to initialize this client from inside of 48 00:03:05,640 --> 00:03:11,310 index not t us and then by the time our ticket created row handler accesses or imports the thing the 49 00:03:11,310 --> 00:03:17,450 client will have already been initialized so as we start to write this code I really just want you to 50 00:03:17,450 --> 00:03:23,780 keep in mind how Mongoose works we are not saying hey create a new instance of a mongoose class or something 51 00:03:23,780 --> 00:03:29,280 like that we just import Mongoose we call connect on it and it seems like everything just works. 52 00:03:29,300 --> 00:03:33,870 Just keep in mind because we want to have an interface that is very similar to mongoose. 53 00:03:33,870 --> 00:03:38,340 OK so with all that having been said let's start to create this Nats client file. 54 00:03:38,390 --> 00:03:41,400 We're actually gonna end up calling this something different we're gonna call it Nats. 55 00:03:41,420 --> 00:03:44,540 Not to us just to stay consistent with the class name. 56 00:03:44,540 --> 00:03:46,450 We're probably gonna end up renaming both these things. 57 00:03:46,450 --> 00:03:50,870 The only reason we're calling it Nats rapper right now is to make its purpose just really clear and 58 00:03:50,900 --> 00:03:53,450 obvious for right now as we are writing this code. 59 00:03:53,540 --> 00:03:53,750 All right. 60 00:03:53,750 --> 00:03:57,480 So back over inside my SRT directory in the ticket's folder. 61 00:03:58,650 --> 00:04:06,580 I'm gonna make a new file called Nats wrapper dot T.S. so inside of here we're going to import Nats 62 00:04:08,000 --> 00:04:11,360 and the Stan type from node Nats streaming 63 00:04:15,060 --> 00:04:21,260 then inside of here will create a class called Nats wrapper and then at the very bottom right away I'm 64 00:04:21,260 --> 00:04:30,120 going to export a single instance of the thing so export concentrates wrapper is new Nats wrapper so 65 00:04:30,140 --> 00:04:32,000 now we can create a client inside of here. 66 00:04:32,030 --> 00:04:37,160 We can assign it as a property to the class and then once we create the instance out of that class we 67 00:04:37,160 --> 00:04:41,780 are gonna share the instance around to all of our different files and we'll be able to say initialize 68 00:04:41,780 --> 00:04:46,880 a client inside there from inside of index to yes and access that instance from inside the ticket created 69 00:04:46,940 --> 00:04:47,570 root handler. 70 00:04:47,570 --> 00:04:51,730 So again just keep mongoose in mind okay. 71 00:04:51,750 --> 00:04:55,350 Before we start to work on the implementation I don't want this video to run too long. 72 00:04:55,350 --> 00:04:57,470 So let's take a quick pause right here.