1 00:00:01,760 --> 00:00:02,030 All right. 2 00:00:02,060 --> 00:00:05,460 Let's continue working on the implementation of this Nat's wrapper class. 3 00:00:05,460 --> 00:00:08,480 So inside of here we want to eventually create a Nats client. 4 00:00:08,570 --> 00:00:11,620 In other words the client that we get back from the Nats library itself. 5 00:00:11,810 --> 00:00:16,070 We're going to eventually create that and assign it as a property it to our class. 6 00:00:16,130 --> 00:00:21,920 So I'm going to add in a private property called client and its type is going to stand and notice some 7 00:00:21,920 --> 00:00:24,050 calling this thing underscore a client. 8 00:00:24,050 --> 00:00:27,880 You'll see why for that or why we're doing that in just a moment. 9 00:00:27,960 --> 00:00:32,460 Now we are not going to try to define this property inside of any constructor or anything like that 10 00:00:32,760 --> 00:00:35,290 because we are running our constructor right here. 11 00:00:35,370 --> 00:00:39,960 And so at this point time it is too early to try to create a actual Nats client and assign it to this 12 00:00:39,960 --> 00:00:41,700 client property instead. 13 00:00:41,700 --> 00:00:46,770 We do not want to create and assign this client property until we eventually tried to call it connect 14 00:00:46,770 --> 00:00:51,070 or something like that from inside of our index start to yes file touch. 15 00:00:51,090 --> 00:00:55,380 However wants to make sure that we tried to either immediately initialize this property on the same 16 00:00:55,380 --> 00:00:58,710 line of code or inside the constructor and that's why we're seeing an error right here. 17 00:00:59,190 --> 00:01:05,190 So to get this air to go away we're gonna say underscore client and then question mark that tells typescript 18 00:01:05,400 --> 00:01:10,520 that this property might be undefined for some periods of time. 19 00:01:10,720 --> 00:01:13,840 After that we're then going to create a function called next. 20 00:01:14,560 --> 00:01:16,630 So again very similar to mongoose. 21 00:01:16,630 --> 00:01:20,680 We're going to eventually call this connect function with our different connection settings from inside 22 00:01:20,680 --> 00:01:24,090 of index dot yes inside of connect. 23 00:01:24,090 --> 00:01:29,850 We're going to create an actual instance of the Nats client and assign it to this underscore client 24 00:01:30,300 --> 00:01:33,990 variable to connect very similar to. 25 00:01:34,000 --> 00:01:37,890 Once again we're going to assume that this thing is going to receive some connection settings. 26 00:01:37,960 --> 00:01:43,030 So for example the cluster idea that we want to connect to the client that we want to name ourselves 27 00:01:43,030 --> 00:01:45,310 as and the URL to try to connect you as well. 28 00:01:45,460 --> 00:01:49,330 Essentially the same connection settings you might recall having written back inside of it Nats test 29 00:01:49,330 --> 00:01:51,830 project inside that listener A.S. file. 30 00:01:51,880 --> 00:01:53,740 So this is what you refer to as our cluster I.D.. 31 00:01:53,740 --> 00:01:55,910 I'll show you where that comes from in just a moment. 32 00:01:55,930 --> 00:02:00,610 We spoke about the client I.D. and then the URL that we actually want to connect to. 33 00:02:00,640 --> 00:02:06,010 So we're going to receive all those three things as arguments to this connect function so say cluster 34 00:02:06,050 --> 00:02:12,280 I.T. going to be a string fine idea will be a string and you l will be a string as well. 35 00:02:13,010 --> 00:02:19,670 Then inside of here we're going to assign this dot underscore client and that's going to come from calling 36 00:02:19,760 --> 00:02:21,220 Nats connect. 37 00:02:21,320 --> 00:02:26,150 We're gonna pass in a cluster I.D. line I.D. and the URL inside of an object. 38 00:02:26,260 --> 00:02:34,030 Make sure you sure you URL is inside of an object like some then normally to somehow get a signal that 39 00:02:34,030 --> 00:02:40,930 we have connected we would write out something like client dot on connect and then put in a callback 40 00:02:40,930 --> 00:02:46,060 function and that callback function would be invoked once Nats actually successfully connects to our 41 00:02:46,060 --> 00:02:51,690 NAT server and so inside that callback function we might do something like a console log of connected 42 00:02:51,750 --> 00:02:54,490 to Nats. 43 00:02:54,570 --> 00:03:00,140 Now this callback semantics right here not that great in today's day and age right. 44 00:03:00,150 --> 00:03:05,460 It'll be a lot better if we could call connect and then be able to use the same kind of async await 45 00:03:05,460 --> 00:03:08,770 syntax as we were able to use with Mongoose connect. 46 00:03:08,850 --> 00:03:14,580 So really we want to somehow take this callback function right here and twist it turn it allow us to 47 00:03:14,580 --> 00:03:19,800 use the async await syntax instead you might recall that we already did something very similar to this 48 00:03:19,800 --> 00:03:24,210 a little bit ago we essentially took the callback function and wrapped it with a promise and then we 49 00:03:24,270 --> 00:03:27,330 manually resolved that promise from inside the callback. 50 00:03:27,330 --> 00:03:29,370 So let's do that same thing right here. 51 00:03:29,370 --> 00:03:32,910 So from connect I'm going to return a new promise 52 00:03:36,370 --> 00:03:41,820 I'm going to put in the resolve and reject all then move in the this dot underscore a client inside 53 00:03:41,830 --> 00:03:50,180 their and then in the success case right after doing that console log I will resolve the promise you'll 54 00:03:50,180 --> 00:03:55,150 notice we have an air around this dot underscore a client we'll just ignore that for a moment now if 55 00:03:55,150 --> 00:03:58,750 we're doing everything correctly we should probably also make sure we handle the heir case the case 56 00:03:58,750 --> 00:04:04,300 in which we fail to connect and that's going to also add in a handler to watch for where we fail to 57 00:04:04,300 --> 00:04:11,610 connect but we're going to do a this dot underscore our clients on air and then put a callback function. 58 00:04:11,730 --> 00:04:15,940 This is how we handle a connection error during the initial connection attempt. 59 00:04:15,990 --> 00:04:20,760 If we fail to connect this callback function will be invoked and the first argument will be some error 60 00:04:20,760 --> 00:04:23,380 describing exactly what went wrong. 61 00:04:23,400 --> 00:04:32,320 So in this case we're going to reject our promise and we will reject it with that error. 62 00:04:32,330 --> 00:04:32,600 All right. 63 00:04:32,600 --> 00:04:37,870 So why is typescript complaining right here if we hover over that thing it says object is possibly undefined. 64 00:04:37,940 --> 00:04:42,960 The reason for this is that typescript understands that we just assigned the client property right here. 65 00:04:42,980 --> 00:04:47,800 So at this one time if we write out this dot underscore client typescript says oh OK sure. 66 00:04:47,810 --> 00:04:53,630 Like Klein exists you just defined it two seconds ago but because we are now trying to access this dot 67 00:04:53,660 --> 00:04:59,750 underscore a client inside of a callback function right here typescript is thinking that we could have 68 00:04:59,750 --> 00:05:06,140 accidentally reassigned or unassigned really the this dot underscore a client between this line of code 69 00:05:06,170 --> 00:05:07,700 and this line of code. 70 00:05:07,730 --> 00:05:12,410 Now that is extremely unlikely to happen but it is what typescript is designed to do. 71 00:05:12,410 --> 00:05:18,530 It is meant to capture errors where a variable might suddenly flip back to going to undefined. 72 00:05:18,680 --> 00:05:22,310 We're going to put in a very temporary fix for this that we're going to change in just a little bit 73 00:05:22,400 --> 00:05:26,570 but I really want to test out the code we put together so far to just get this aired to go away. 74 00:05:26,600 --> 00:05:33,580 We're gonna put a exclamation right after this dot underscore a client on both those okay. 75 00:05:33,580 --> 00:05:34,520 So let's say this. 76 00:05:34,780 --> 00:05:39,520 And I would now like to do a quick test and just make sure that we can actually connect to Nats. 77 00:05:39,700 --> 00:05:46,060 We want to be able to call this connect function and use the async await syntax on it so to do so we'll 78 00:05:46,060 --> 00:05:50,390 go back over to index dot s at the very top. 79 00:05:50,420 --> 00:05:56,210 I'm going to import Nats rapper from NATS rapper. 80 00:05:56,240 --> 00:05:59,180 Notice I'm using a lowercase and right here. 81 00:05:59,180 --> 00:06:03,650 The reason for that is that we are indicating that this is an instance of the class and that's rapper. 82 00:06:03,680 --> 00:06:08,010 We only have one instance floating around the entire application because we are not exporting the class. 83 00:06:08,030 --> 00:06:14,240 We are exporting one single instance that will be shared between all of our different files so down 84 00:06:14,240 --> 00:06:16,060 here right before our Mongoose dot connect. 85 00:06:16,070 --> 00:06:22,820 I'm going to put in and await Nats rapper not connect and we'll put in those three connection settings 86 00:06:23,330 --> 00:06:26,870 the first one is going to be our cluster I.D. and I told you that I would tell you where that is coming 87 00:06:26,870 --> 00:06:27,200 from. 88 00:06:27,200 --> 00:06:32,240 So let's do that really quickly if we think back to the Nats test project and open up that listener 89 00:06:32,510 --> 00:06:36,690 you'll recall that for that cluster I.D. or the first argument we put in a string of ticketing. 90 00:06:36,740 --> 00:06:37,610 So why is that. 91 00:06:38,420 --> 00:06:46,410 Well if we think back also to our infra directory inside there we had the Nats deployment file so here's 92 00:06:46,420 --> 00:06:47,540 the Nats deployment file. 93 00:06:47,590 --> 00:06:53,570 Here's our pod spec and here's the arguments we fed to that container one of the arguments we provided 94 00:06:53,570 --> 00:06:56,140 was see I.D. and we give it a value of ticketing. 95 00:06:56,260 --> 00:06:58,310 See I.D. stands for a cluster I.D.. 96 00:06:58,310 --> 00:07:01,570 This is going to assign an I.D. to the cluster in our case. 97 00:07:01,580 --> 00:07:07,100 The idea simply ticketing that's what the first argument is we're specifying the idea of the cluster 98 00:07:07,100 --> 00:07:08,520 that we're trying to connect to. 99 00:07:08,750 --> 00:07:15,920 It's open and ticketing or that first arguments then the second argument you'll recall is going to be 100 00:07:15,920 --> 00:07:20,760 our client I.D. We need to make sure that this is a more or less random value. 101 00:07:20,960 --> 00:07:24,420 Right now we've just put in some kind of random string. 102 00:07:24,630 --> 00:07:29,160 And then finally we have to put into your all that we want to connect to ask the third arguments the 103 00:07:29,160 --> 00:07:35,210 world that we going to connect to is going to be at the service that is governing access to our Nats 104 00:07:35,210 --> 00:07:36,190 deployments. 105 00:07:36,210 --> 00:07:41,460 Remember we defined that service at the bottom of this file its name was Nats SRB and we specifically 106 00:07:41,460 --> 00:07:47,870 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 107 00:07:48,380 --> 00:07:57,190 H GDP calling slash slash Nats SRB colon 4 2 2 2 OK. 108 00:07:57,210 --> 00:08:00,970 So that should be enough to at least test this out and make sure that we can actually connect to it. 109 00:08:00,990 --> 00:08:03,360 That's going to save this. 110 00:08:03,380 --> 00:08:07,790 And then going to go back over to my scaffold window which I'm still running at my terminal and I'll 111 00:08:07,790 --> 00:08:10,030 see it connected to nets. 112 00:08:10,160 --> 00:08:14,960 Now if you failed to connect for any reason I would really encourage you to take a look at that message. 113 00:08:15,080 --> 00:08:20,560 Also if you don't see anything around that's down here try stopping scaffold and restarting it. 114 00:08:20,830 --> 00:08:26,010 Now if you see an error I would suspect that you might have a duplicate client idea news. 115 00:08:26,030 --> 00:08:31,250 So try changing the client idea to something else saving it and that should get you your automatic refresh 116 00:08:31,490 --> 00:08:36,440 and then you should see connected to Nats if you still do not have managed to have any success. 117 00:08:36,440 --> 00:08:37,070 Don't sweat it. 118 00:08:37,140 --> 00:08:43,570 HOPPER The a post your code on github link me to your code and I'll do my best to help you debug this. 119 00:08:43,660 --> 00:08:43,920 All right. 120 00:08:43,960 --> 00:08:49,120 So it looks like we were able to connect to Nats and most importantly we were able to do so using some 121 00:08:49,120 --> 00:08:52,410 semantics that look very similar to Mongoose. 122 00:08:52,460 --> 00:08:53,690 So this is definitely good. 123 00:08:53,740 --> 00:08:58,840 But now I'm kind of curious how are we going to actually use this thing from inside of say our new root 124 00:08:58,840 --> 00:09:00,670 handler where we are creating a ticket. 125 00:09:00,710 --> 00:09:02,340 So still a little bit of work to do. 126 00:09:02,350 --> 00:09:04,470 Let's take a pause right here and return in just a moment.