1 00:00:00,870 --> 00:00:05,070 The publisher is all put together so let's now work on implementation for our listener. 2 00:00:05,100 --> 00:00:06,700 The goal of our listener. 3 00:00:06,900 --> 00:00:10,860 We're going to come up with a subject name or the channel that we want to listen to. 4 00:00:10,860 --> 00:00:15,230 We're gonna pass that into the stand client to create a subscription and then we're going to watch this 5 00:00:15,230 --> 00:00:21,640 subscription or anytime it receives some data to back inside of my listener T.S. file. 6 00:00:21,670 --> 00:00:29,030 At the very top we're going to import Nats once again from node that streaming will then create my clients 7 00:00:29,090 --> 00:00:35,900 which remember we referred to Muni convention as simply Stan so that's dot connect to once again put 8 00:00:35,900 --> 00:00:38,330 in some kind of mysterious looking arguments right here. 9 00:00:38,360 --> 00:00:41,640 Just bear with me for a moment we will come back to the Connect function. 10 00:00:41,640 --> 00:00:43,260 It's going to put in ticketing. 11 00:00:43,480 --> 00:00:51,070 I'm going to put in one two three and then an object and inside there will be a your l and very similar 12 00:00:51,070 --> 00:00:56,440 to what we had last time we'll put an H TTP local host or at 4 2 2 2 13 00:00:59,600 --> 00:01:02,600 0 then watch for a Connect event 14 00:01:05,640 --> 00:01:10,130 so after we have successfully connected once again let's just do a quick console log and say hey our 15 00:01:10,130 --> 00:01:20,480 listener connected to Nats I'll do a console log of listener connected to Nats let's now go over to 16 00:01:20,480 --> 00:01:25,410 our terminal and just tried to run this program and make sure that the listener can connect as well. 17 00:01:26,210 --> 00:01:30,210 So back at my terminal I'm going to find a window where I was running the publisher. 18 00:01:30,390 --> 00:01:35,220 If you are running a terminal where you can do a split screen I highly recommend you tried to pull up 19 00:01:35,250 --> 00:01:36,640 a split screen right now. 20 00:01:36,690 --> 00:01:43,370 So for me I'm making use of item two on Mac OS a hotkey to do that is command D so I can hit command 21 00:01:43,370 --> 00:01:43,710 D. 22 00:01:43,770 --> 00:01:48,810 And now I get the split screen right here so this will allow us to watch events being published and 23 00:01:48,840 --> 00:01:50,980 listen for at the same time. 24 00:01:51,120 --> 00:01:56,100 If you do not have a terminal where you can do a split screen no problem just open up a second terminal 25 00:01:56,100 --> 00:01:59,400 window and maybe drag them next to each other just like this. 26 00:01:59,410 --> 00:02:05,960 You can see the events being published and listened to at the same time then inside this second window. 27 00:02:06,280 --> 00:02:14,500 I'm still inside of my Nats dash test directory and inside there I will run NPM run listener to me NPM 28 00:02:14,560 --> 00:02:20,920 run listen we gave it a slightly different script name so NPM run listen and it looks like all right. 29 00:02:20,920 --> 00:02:23,490 We were able to connect to Nats at this point. 30 00:02:23,500 --> 00:02:29,470 We've got a lot of extra information inside of here so this restart stuff typescript log in information 31 00:02:29,470 --> 00:02:30,490 and so on. 32 00:02:30,490 --> 00:02:35,520 So really quickly I can go back to both these scripts and right after the import statements I'm going 33 00:02:35,520 --> 00:02:41,800 to add in a console dot clear I'm gonna put them inside of listener and I'm going to put it inside of 34 00:02:41,800 --> 00:02:42,990 the publisher as well. 35 00:02:45,550 --> 00:02:51,970 Right after the import statement now whenever this stuff runs or whenever our code gets reran because 36 00:02:51,970 --> 00:02:56,530 we change the file now we'll just see the console logs at the very top and just remove a lot of the 37 00:02:56,530 --> 00:03:01,710 garbage that was in here previously one other thing I wanna mention very quickly and this is gonna become 38 00:03:01,740 --> 00:03:05,580 very relevant as we're gonna want to start to publish a lot of events here in just a moment. 39 00:03:05,970 --> 00:03:11,880 If we ever want to restart our program while we are using T.S. no Dev we can select the terminal window 40 00:03:11,940 --> 00:03:18,540 that's running our program type in the letters R S and then hit enter and that will automatically restart 41 00:03:18,600 --> 00:03:19,790 our program. 42 00:03:19,810 --> 00:03:25,680 So right now every single time I type in RSS and then hit enter I am restarting my program which means 43 00:03:25,710 --> 00:03:30,750 I'd create a new connection to the Net server and then I publish a brand new event. 44 00:03:30,750 --> 00:03:37,920 So if I do this a couple more times I've now probably published like five six or seven different events 45 00:03:38,220 --> 00:03:44,450 on that particular channel the channel with the subject of ticket colon created OK. 46 00:03:44,490 --> 00:03:49,010 So let's now go back over and work on our listener implementation inside of listener. 47 00:03:49,050 --> 00:03:49,820 Yes. 48 00:03:49,830 --> 00:03:54,240 After we successfully connect we're going to come up with the name of the subject that we want to listen 49 00:03:54,240 --> 00:04:00,740 to and use it to create a subscription so I'm going to write right after the council log on to subscription 50 00:04:01,460 --> 00:04:12,010 is Stan dot subscribe and we're gonna put in our subject right here which is ticket colon created then 51 00:04:12,070 --> 00:04:13,690 after creating the subscription. 52 00:04:13,750 --> 00:04:18,280 Remember this is the object that we're going to listen to and we're going to receive some data through 53 00:04:18,280 --> 00:04:24,560 the subscription contrary to how many other libraries work where you might be used to adding in a callback 54 00:04:24,560 --> 00:04:29,680 function like that that would be invoked with whatever data we receive not how it works with no dates 55 00:04:30,040 --> 00:04:30,780 dreaming. 56 00:04:30,800 --> 00:04:36,200 Instead we create the subscription first and then listen to a very particular type of event off the 57 00:04:36,260 --> 00:04:37,360 subscription itself. 58 00:04:38,190 --> 00:04:47,620 We're going to call subscription dot on message so as I mentioned at the end of last video in the world 59 00:04:47,620 --> 00:04:51,880 of Nats they don't really refer to these events being exchanged as events. 60 00:04:51,880 --> 00:04:55,120 Instead they referred to as messages you'll see this message. 61 00:04:55,120 --> 00:05:01,270 Terminology floating around a little bit then the argument to this is going to be the actual message 62 00:05:01,900 --> 00:05:05,410 which we frequently abbreviate as simply NSG. 63 00:05:05,660 --> 00:05:07,910 It must be right here is not just the raw data. 64 00:05:07,940 --> 00:05:10,570 Instead it's got a couple of different properties inside of it. 65 00:05:10,580 --> 00:05:15,350 We'll take a look at over time so right now let's just do a console log of. 66 00:05:15,620 --> 00:05:20,760 Message received so we're not actually looking at that message. 67 00:05:20,760 --> 00:05:23,490 We're not actually trying to inspect any of the data inside it just yet. 68 00:05:23,530 --> 00:05:28,040 I just want to make sure that we can actually receive a message that's all OK. 69 00:05:28,070 --> 00:05:29,480 I'm going to save this. 70 00:05:29,540 --> 00:05:31,020 I can make sure both files are saved. 71 00:05:31,160 --> 00:05:34,440 And then I'll go back over to my terminal. 72 00:05:34,570 --> 00:05:40,540 Now in theory our listener is creating a subscription and listening to that very specific channel. 73 00:05:40,540 --> 00:05:42,490 So let's now go back over to our publisher. 74 00:05:42,490 --> 00:05:46,500 We're going to restart the publisher and attempt to publish another event. 75 00:05:46,630 --> 00:05:51,970 The instant we do so and I really do mean the very instant we do so we should see a console log from 76 00:05:51,970 --> 00:05:54,880 the listener saying that it received a message. 77 00:05:54,940 --> 00:05:57,370 So let's try this out on the publisher. 78 00:05:57,360 --> 00:06:01,060 I'm gonna put an R S hit enter and there we go. 79 00:06:01,060 --> 00:06:03,320 We have received that message. 80 00:06:03,340 --> 00:06:03,540 OK. 81 00:06:03,580 --> 00:06:10,460 So that's a very basic chain of publishing some data and receiving some data as well now. 82 00:06:10,470 --> 00:06:13,790 Everything at this point really seems pretty simple and straightforward. 83 00:06:13,800 --> 00:06:14,540 Right. 84 00:06:14,570 --> 00:06:16,950 It's pretty much a basic eventing setup. 85 00:06:16,950 --> 00:06:18,600 Nothing too crazy. 86 00:06:18,600 --> 00:06:21,960 You might you think at this point that I'm going really slowly and he might be screaming at the screen 87 00:06:21,960 --> 00:06:27,060 saying Steven can you like hurry it up and get to the point well believe you me we were going slow for 88 00:06:27,120 --> 00:06:31,320 a reason it's really important for you to understand these very basic concepts because in the coming 89 00:06:31,320 --> 00:06:35,670 videos we are going to absolutely blow the lid on complexity here. 90 00:06:35,760 --> 00:06:40,680 We are really going to start to plumb the depths of gnats and really understand what is going on behind 91 00:06:40,680 --> 00:06:47,460 the scenes because as you will see very very quickly some very small settings inside of here is going 92 00:06:47,460 --> 00:06:50,880 to dramatically change how our application works. 93 00:06:50,880 --> 00:06:52,580 So I'm really excited for the coming videos. 94 00:06:52,620 --> 00:06:54,890 So quick browser here and we'll continue in just a moment.