1 00:00:01,060 --> 00:00:01,750 Enough lecturing. 2 00:00:01,750 --> 00:00:03,340 Time to start writing out some code. 3 00:00:03,400 --> 00:00:04,800 So here's our short term goal. 4 00:00:04,810 --> 00:00:09,040 Here's where we're going to go through the next couple of videos we're gonna create a new sub project 5 00:00:09,220 --> 00:00:11,530 inside of our current project directory. 6 00:00:11,650 --> 00:00:15,370 We're going to make sure that it has types of script support because I want to make sure that we understand 7 00:00:15,370 --> 00:00:18,200 how to work with no not streaming while using typescript. 8 00:00:18,670 --> 00:00:22,780 We're then going to install no net streaming into it and we're going to use it to connect toward that 9 00:00:22,780 --> 00:00:28,280 streaming server then inside this little sub project we're going to make sure that we have two different 10 00:00:28,340 --> 00:00:31,820 NPM scripts essentially two different ways to run our project. 11 00:00:31,820 --> 00:00:34,980 One script is going to try to run some code to emit events. 12 00:00:35,030 --> 00:00:39,680 Internet streaming server and the other script is going to run some code that's going to listen for 13 00:00:39,680 --> 00:00:45,740 events so we'll then have essentially two separate programs where we can emit events or listen for events 14 00:00:47,480 --> 00:00:51,800 last important note that won't throw out is that this program or essentially is two separate programs 15 00:00:52,040 --> 00:00:55,010 are going to be executed from outside of Cuban settings. 16 00:00:55,160 --> 00:00:58,820 So we're not going to worry about creating a deployment or anything like that. 17 00:00:58,820 --> 00:01:03,620 However this is going to present some interesting challenge because we're going to want to connect to 18 00:01:03,620 --> 00:01:07,910 the node that streams server or the net streaming server that is running inside of our Cuban cities 19 00:01:07,940 --> 00:01:08,720 cluster. 20 00:01:08,780 --> 00:01:13,830 So we just need to keep in mind that we need to somehow get access to the Nat streaming server. 21 00:01:14,030 --> 00:01:16,500 So let's get to it to get started. 22 00:01:16,520 --> 00:01:18,090 I can go back over to my terminal. 23 00:01:18,220 --> 00:01:24,620 I'm inside of my route ticketing directory inside of you're going to make a folder called Nat's test 24 00:01:25,640 --> 00:01:30,320 the only reason I'm calling it Dash test is to just make it really clear that this is some little test 25 00:01:30,320 --> 00:01:37,220 project inside of your going to change into that directory generate a package not just on file 26 00:01:40,190 --> 00:01:42,760 will then install a couple of different dependencies. 27 00:01:42,890 --> 00:01:46,120 So in total we're going to get that node and that's streaming thing. 28 00:01:46,160 --> 00:01:51,380 We're also going to get a couple of other dependencies just to run this project using typescript so 29 00:01:51,500 --> 00:02:02,910 npm install we're going to get node Nats streaming T.S. node Dev TypeScript and we're also gonna get 30 00:02:02,940 --> 00:02:10,700 a tight definition file for node J.S. itself at types flash node. 31 00:02:10,950 --> 00:02:15,920 Let's run that it's gonna take just a moment or two to install all that stuff. 32 00:02:15,920 --> 00:02:22,750 So while that's running let's go back over to our Ed and find the new Nats dash test directory inside 33 00:02:22,800 --> 00:02:31,840 they're going to make a new folder called as RC and inside of SLC I'm going to create two files. 34 00:02:32,040 --> 00:02:40,470 I'm going to call one publisher that T.S. and the other listener got to yes so these are going to be 35 00:02:40,470 --> 00:02:43,330 essentially the two separate programs we're going to put together. 36 00:02:43,350 --> 00:02:48,840 One is going to be dedicated to listening for events as it guess that's listener dot to us and the other 37 00:02:48,840 --> 00:02:51,200 is going to be dedicated to publishing events. 38 00:02:51,210 --> 00:02:56,330 Obviously publisher T.S. before worrying about writing out any code inside of here. 39 00:02:56,350 --> 00:03:01,710 Let's write out some different scripts to run each of these two separate files inside a package. 40 00:03:01,730 --> 00:03:02,470 Jason. 41 00:03:02,720 --> 00:03:07,290 I will delete the test script and I'm going to replace it with publish 42 00:03:11,160 --> 00:03:12,660 whenever we run the published commands. 43 00:03:12,660 --> 00:03:16,450 We're going to execute T.S. node Dev. 44 00:03:16,560 --> 00:03:23,210 We're gonna put in a option here of dash dash notify false and then we're going to try to run the SDR 45 00:03:23,220 --> 00:03:31,170 see publisher dot t s file then as a second script we're going to do pretty much the same thing. 46 00:03:31,240 --> 00:03:34,180 It will change the script name to listen. 47 00:03:34,190 --> 00:03:34,790 Still gonna do. 48 00:03:34,820 --> 00:03:35,020 Yes. 49 00:03:35,030 --> 00:03:39,330 No Dev dash dash notify false our. 50 00:03:39,340 --> 00:03:41,260 And this time around listener dot. 51 00:03:41,300 --> 00:03:41,680 Yes 52 00:03:44,560 --> 00:03:45,470 yeah that looks good. 53 00:03:47,520 --> 00:03:50,180 So we've got some root files we've got dependencies. 54 00:03:50,200 --> 00:03:52,810 We've got our scripts before we start to write out some code. 55 00:03:52,810 --> 00:03:55,040 One last little administrative thing we have to do. 56 00:03:55,240 --> 00:03:56,530 We are making use of typescript. 57 00:03:56,560 --> 00:04:01,840 So we really should still create a T S config file inside of here to do so we can't go back over to 58 00:04:01,840 --> 00:04:08,390 our terminal and again inside of our Nat stash test directory a TSC dash dash in it. 59 00:04:08,390 --> 00:04:13,340 And of course this command is assuming that you have typescript globally installed on your system. 60 00:04:13,340 --> 00:04:19,040 So if you get a an error from that you'll need to do an npm install DST type TypeScript and then rerun 61 00:04:19,040 --> 00:04:22,840 TSC dash dash in it OK. 62 00:04:22,890 --> 00:04:25,080 So let's go back over. 63 00:04:25,230 --> 00:04:30,910 We're gonna first start off inside of our publisher dot t yes file inside of you're at the very top. 64 00:04:30,950 --> 00:04:34,200 Let's first import the node Nat streaming library. 65 00:04:34,320 --> 00:04:41,700 So we're going to import nets from node Nats streaming we're then going to use this library to create 66 00:04:41,760 --> 00:04:42,690 a client. 67 00:04:42,810 --> 00:04:47,280 The client is what is going to actually connect to our Nat streaming server and tried to exchange some 68 00:04:47,280 --> 00:04:52,810 information with its will say client is Nats dot connect. 69 00:04:52,810 --> 00:04:57,580 Now quick note here you're going to see inside of just about all the documentation rather than calling 70 00:04:57,580 --> 00:05:00,810 this thing client which is a very reasonable term for it. 71 00:05:00,830 --> 00:05:04,530 Instead a lot of documentation is gonna refer to this as Stan. 72 00:05:04,720 --> 00:05:11,650 So Stan is just kind of a nat streaming server or Nats project little piece of terminology is just what 73 00:05:11,650 --> 00:05:19,890 they like to call a client Nats as you'd kind of guess is simply to me or Stan is just Nats backwards. 74 00:05:20,110 --> 00:05:23,170 So Nats is the library we're kind of a class of sorts. 75 00:05:23,170 --> 00:05:29,110 Stan is an actual instance or a client that we're using to connect to Nat streaming server if you want 76 00:05:29,110 --> 00:05:32,240 to instead called his client because it's a thousand times more clear. 77 00:05:32,290 --> 00:05:33,190 That's totally fine. 78 00:05:33,190 --> 00:05:39,610 But I'm going to stick with the community convention which is to call it Stan it's now inside of connect 79 00:05:39,610 --> 00:05:41,470 right here we're getting an air of course. 80 00:05:41,470 --> 00:05:44,520 So we need to put in a couple of different arguments to this thing. 81 00:05:44,710 --> 00:05:48,010 The first two arguments are gonna put in where it's going to throw in four right now and we'll come 82 00:05:48,010 --> 00:05:50,330 back and discuss why we use these values. 83 00:05:50,330 --> 00:05:52,000 And just a little bit. 84 00:05:52,230 --> 00:05:58,190 The first thing I'm gonna put in is a string of ticketing but a second argument I'm gonna put an ABC 85 00:05:59,310 --> 00:06:05,700 and then finally the last argument I'm gonna put in an object to specify a URL and I'll put it in a 86 00:06:05,700 --> 00:06:11,170 address right here of H TTP and slash slash local post or to do to 87 00:06:14,810 --> 00:06:17,210 then after that we're going to wait. 88 00:06:17,270 --> 00:06:22,850 Or Stan or the client in this case to successfully connect to our Nat streaming server. 89 00:06:22,850 --> 00:06:24,560 So to wait for this thing to actually connect. 90 00:06:24,590 --> 00:06:28,710 Unfortunately again we don't get to make use of that nice async await syntax. 91 00:06:28,730 --> 00:06:32,480 Instead we have to take a primarily event driven approach. 92 00:06:32,480 --> 00:06:38,090 So after the client successfully connects to the Natsumi server is going to emit a Connect event. 93 00:06:38,210 --> 00:06:40,500 So we're going to listen for the Connect event. 94 00:06:40,650 --> 00:06:46,820 I'm going to put in Stan dot on next and then a function as a second argument. 95 00:06:47,390 --> 00:06:52,970 So this function will be executed after the client has successfully connected to the Nat streaming server. 96 00:06:53,060 --> 00:06:57,750 Let's do a console log inside of here just to confirm that that takes place. 97 00:06:58,020 --> 00:07:05,100 So do a console log of publisher connected to that's it. 98 00:07:05,150 --> 00:07:06,440 So this looks good. 99 00:07:06,440 --> 00:07:07,790 Let's save this right here. 100 00:07:07,790 --> 00:07:11,900 If we try to run this code right now we're going to get an error because our net streaming server is 101 00:07:11,900 --> 00:07:18,160 not available on localized or two to two remember answering server is running inside of our communities 102 00:07:18,170 --> 00:07:22,990 cluster and by default we don't have direct access to anything running inside there. 103 00:07:23,030 --> 00:07:23,970 So let's take a pause right here. 104 00:07:23,990 --> 00:07:27,890 We'll come back next video and figure out how we can somehow get access to the Nat streaming server 105 00:07:28,160 --> 00:07:30,550 very easily just for test purposes.