1 00:00:00,770 --> 00:00:05,790 In the last video we set up a subscription inside of our listener and we said that anytime we received 2 00:00:05,820 --> 00:00:10,140 a message over the channel of ticket colon created we were going to simply counts log. 3 00:00:10,140 --> 00:00:11,760 Message received. 4 00:00:11,760 --> 00:00:18,150 If we go back over to our terminal we can select the publisher window and put in our s. a couple of 5 00:00:18,150 --> 00:00:23,400 times to restart this thing and every single time we do we are publishing a brand new event. 6 00:00:23,580 --> 00:00:29,940 And clearly the listener is receiving that every single time but right now we're just doing a very simple 7 00:00:29,940 --> 00:00:32,090 console log of Message received. 8 00:00:32,100 --> 00:00:35,830 Let's try to print out some more information about the message that was actually received. 9 00:00:35,940 --> 00:00:41,600 In other words let's actually access the data inside there and try to console log it out back inside 10 00:00:41,610 --> 00:00:45,640 my listener test file I'm going to take a look at that callback function right there. 11 00:00:46,490 --> 00:00:53,090 So we receive a argument of message to the thing which we usually abbreviate simply as m as G if we 12 00:00:53,090 --> 00:00:59,120 hover over that right now typescript thinks that it is of type any we're gonna give typescript a little 13 00:00:59,120 --> 00:01:02,720 bit of help we're going to tell typescript that this is not a type any. 14 00:01:02,720 --> 00:01:08,990 Instead it is something of type message to do so we're going to first import the message type from the 15 00:01:09,080 --> 00:01:10,840 node Nat streaming library. 16 00:01:10,940 --> 00:01:18,960 So at the very top we will imports message like so this is an interface that's going to describe the 17 00:01:18,960 --> 00:01:21,870 type of this argument right here. 18 00:01:21,870 --> 00:01:30,490 So I'm going to put in a type imitation of message like so I'm then going to do a command click on Message 19 00:01:30,550 --> 00:01:34,810 so we can take a look at the type definition for it so we can understand what properties are available 20 00:01:34,810 --> 00:01:40,140 inside their that's going to take us to the type definition file for the node Nat streaming library 21 00:01:40,470 --> 00:01:42,690 that we already looked at one or two videos ago. 22 00:01:42,840 --> 00:01:49,470 Now just so you know there is documentation around node Nat streaming over on NPM J.S. dot com but a 23 00:01:49,470 --> 00:01:54,420 lot of the documentation on here is really about some nitty gritty aspects of the library as opposed 24 00:01:54,420 --> 00:01:57,270 to the individual methods we have access to. 25 00:01:57,300 --> 00:02:01,530 So to get some actual documentation and understand the different methods properties and so on. 26 00:02:01,530 --> 00:02:07,620 Do we have access to we're going to spend a lot of time looking at the type definition file so inside 27 00:02:07,620 --> 00:02:10,220 of here we can see exactly what a message is. 28 00:02:10,440 --> 00:02:14,100 A message is going to have a couple of different properties associated with it. 29 00:02:14,100 --> 00:02:18,330 All these are different functions that you and I are going to call to access some information included 30 00:02:18,330 --> 00:02:19,440 in the message. 31 00:02:19,680 --> 00:02:24,600 Some of these different methods we don't really care about at all but there are a couple that are extremely 32 00:02:24,600 --> 00:02:26,790 important for us at this point. 33 00:02:26,790 --> 00:02:29,910 I want to point out two or three that are really important. 34 00:02:29,910 --> 00:02:35,340 The first one is get subject right here Get subject is going to return the name of the channel that 35 00:02:35,340 --> 00:02:37,280 this message just came out of. 36 00:02:37,320 --> 00:02:42,660 Now this one isn't super important for us because well we already know the name of the channel because 37 00:02:42,660 --> 00:02:43,700 we just subscribe to it. 38 00:02:43,700 --> 00:02:44,150 Right. 39 00:02:44,160 --> 00:02:51,800 Two seconds ago if we called the Get subject method on message we would just get back that right there. 40 00:02:51,800 --> 00:02:56,310 That same exact string is just the name of the channel that we got the message from. 41 00:02:56,310 --> 00:03:02,850 So a little bit more relevant for us are the methods of get sequence and get data get sequence is going 42 00:03:02,850 --> 00:03:09,130 to return the number of this event in that's all events start off at number one. 43 00:03:09,150 --> 00:03:13,890 In other words the very first event that we emit on a channel will be assigned a sequence number of 44 00:03:13,890 --> 00:03:14,570 one. 45 00:03:14,640 --> 00:03:20,790 The second event we met on a channel be given a sequence number of two three four and so on. 46 00:03:20,820 --> 00:03:23,420 The other method that's very important is get data. 47 00:03:23,430 --> 00:03:25,210 So this is going to return the actual data. 48 00:03:25,230 --> 00:03:28,520 That's included inside the message or what we refer to as an event. 49 00:03:28,980 --> 00:03:34,740 So in our case back inside of our publisher we have some data that we're sending inside of this message 50 00:03:35,190 --> 00:03:36,520 of some tickets. 51 00:03:36,540 --> 00:03:41,160 So if we try to pull our data using that get data method out of the message inside of our listener we 52 00:03:41,160 --> 00:03:44,550 should get back something that looks like this right here. 53 00:03:44,570 --> 00:03:49,160 Do you remember that all information in that streaming is exchanged as strings for us. 54 00:03:49,160 --> 00:03:53,920 And so we threw this object in as Jason into that event. 55 00:03:54,050 --> 00:03:59,650 And so if we tried to extract information back inside of our listener from this message we're gonna 56 00:03:59,660 --> 00:04:00,730 get that same object. 57 00:04:00,740 --> 00:04:05,930 But in Jason form which just means we're going to have to run a Jason pass on it and the other thing 58 00:04:05,930 --> 00:04:10,940 I want to point out is that they get data method is going to return either a string or a buffer. 59 00:04:10,940 --> 00:04:15,090 So as far as typescript is concerned teacher wants us to make sure that we're actually working with 60 00:04:15,090 --> 00:04:15,670 the string. 61 00:04:15,740 --> 00:04:17,370 When we call get data. 62 00:04:17,400 --> 00:04:17,670 OK. 63 00:04:17,710 --> 00:04:23,180 So with all this in mind let's go back to our listener inside of our little callback right there. 64 00:04:23,180 --> 00:04:31,290 I'm going to remove that console log I'm going to pull our data out of the message and so again if we 65 00:04:31,290 --> 00:04:35,110 now hover over the thing it's going to tell us that it's either a string or a buffer. 66 00:04:35,130 --> 00:04:38,760 We have to put it a little tight check to make sure we're actually working with the string. 67 00:04:38,760 --> 00:04:42,050 We're not really going to ever be worrying or dealing with buffers for us. 68 00:04:42,060 --> 00:04:43,560 It's just about always going to be a string. 69 00:04:43,620 --> 00:04:49,940 We're just putting in this check right here to make typescript happy they'll say if type of data is 70 00:04:49,940 --> 00:04:50,800 a string. 71 00:04:50,880 --> 00:04:55,010 Now let's go ahead and try to pass it and console log out some information. 72 00:04:55,200 --> 00:05:02,470 So I gonna do a console log I'm going to put a template string inside of here and I'll console log received 73 00:05:03,190 --> 00:05:10,080 event number and I'm going to try to print out the number of this event or its sequence number so I'll 74 00:05:10,080 --> 00:05:11,890 do some string interpolation. 75 00:05:11,900 --> 00:05:13,900 Inside there I'll put in NSG. 76 00:05:14,010 --> 00:05:15,090 Get sequence 77 00:05:18,190 --> 00:05:27,540 and then a comma right after let's say with data and then I'll try to pass the data from the Jason that 78 00:05:27,540 --> 00:05:28,080 it currently is. 79 00:05:28,110 --> 00:05:30,680 So I'll do a another bit of string interpolation. 80 00:05:30,780 --> 00:05:37,160 And Jason does pass data I'm going to save this just so it wraps that string around. 81 00:05:37,210 --> 00:05:40,010 There we go. 82 00:05:40,020 --> 00:05:40,250 All right. 83 00:05:40,350 --> 00:05:41,800 So let's make sure we say this. 84 00:05:41,820 --> 00:05:47,410 We're then going to go back over to a terminal and tested out so as soon as our listener restarts. 85 00:05:47,430 --> 00:05:49,970 Well we have not yet received an event if we want to test this thing. 86 00:05:49,980 --> 00:05:51,720 We need to publish another event. 87 00:05:51,720 --> 00:05:57,030 So we'll go back over the publisher window but in the RSS hit enter and now we'll see right there received 88 00:05:57,030 --> 00:05:58,980 event 18 with data. 89 00:05:59,190 --> 00:06:05,280 And unfortunately our data was turned into object object just an oddity of console logging with no J.S. 90 00:06:06,120 --> 00:06:07,430 rather than passing the Jason out. 91 00:06:07,440 --> 00:06:10,560 Let's just leave it as a string so we can make sure that the raw data is inside there. 92 00:06:11,370 --> 00:06:18,910 So back at this console log I will remove the Jason pass and just console log the data itself I'll say 93 00:06:18,930 --> 00:06:24,480 this go back over I'm going to emit another event by doing another R S on the left. 94 00:06:24,690 --> 00:06:25,540 And that's better. 95 00:06:25,540 --> 00:06:29,070 So now I see it received event number 19 with that data. 96 00:06:29,190 --> 00:06:33,570 If I start to publish a couple more events by restarting at the publisher you're going to see that that 97 00:06:33,570 --> 00:06:42,530 sequence number gets incremented every single time so event number 19 20 21 22 23 and so on. 98 00:06:42,530 --> 00:06:43,280 Go. 99 00:06:43,370 --> 00:06:43,610 All right. 100 00:06:43,610 --> 00:06:47,450 So just a little bit more of terminology and how we work with the incoming message. 101 00:06:47,810 --> 00:06:51,830 I did promise at the end of last video that things were about to get all that crazy doesn't seem like 102 00:06:51,830 --> 00:06:52,180 it yet. 103 00:06:52,190 --> 00:06:52,520 Right. 104 00:06:52,580 --> 00:06:54,950 Okay well just give me another video or two so quick pause. 105 00:06:55,010 --> 00:06:55,870 I'll see you in just a minute.