In the last video we set up a subscription inside of our listener and we said that anytime we received
a message over the channel of ticket colon created we were going to simply counts log.
Message received.
If we go back over to our terminal we can select the publisher window and put in our s. a couple of
times to restart this thing and every single time we do we are publishing a brand new event.
And clearly the listener is receiving that every single time but right now we're just doing a very simple
console log of Message received.
Let's try to print out some more information about the message that was actually received.
In other words let's actually access the data inside there and try to console log it out back inside
my listener test file I'm going to take a look at that callback function right there.
So we receive a argument of message to the thing which we usually abbreviate simply as m as G if we
hover over that right now typescript thinks that it is of type any we're gonna give typescript a little
bit of help we're going to tell typescript that this is not a type any.
Instead it is something of type message to do so we're going to first import the message type from the
node Nat streaming library.
So at the very top we will imports message like so this is an interface that's going to describe the
type of this argument right here.
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
so we can take a look at the type definition for it so we can understand what properties are available
inside their that's going to take us to the type definition file for the node Nat streaming library
that we already looked at one or two videos ago.
Now just so you know there is documentation around node Nat streaming over on NPM J.S. dot com but a
lot of the documentation on here is really about some nitty gritty aspects of the library as opposed
to the individual methods we have access to.
So to get some actual documentation and understand the different methods properties and so on.
Do we have access to we're going to spend a lot of time looking at the type definition file so inside
of here we can see exactly what a message is.
A message is going to have a couple of different properties associated with it.
All these are different functions that you and I are going to call to access some information included
in the message.
Some of these different methods we don't really care about at all but there are a couple that are extremely
important for us at this point.
I want to point out two or three that are really important.
The first one is get subject right here Get subject is going to return the name of the channel that
this message just came out of.
Now this one isn't super important for us because well we already know the name of the channel because
we just subscribe to it.
Right.
Two seconds ago if we called the Get subject method on message we would just get back that right there.
That same exact string is just the name of the channel that we got the message from.
So a little bit more relevant for us are the methods of get sequence and get data get sequence is going
to return the number of this event in that's all events start off at number one.
In other words the very first event that we emit on a channel will be assigned a sequence number of
one.
The second event we met on a channel be given a sequence number of two three four and so on.
The other method that's very important is get data.
So this is going to return the actual data.
That's included inside the message or what we refer to as an event.
So in our case back inside of our publisher we have some data that we're sending inside of this message
of some tickets.
So if we try to pull our data using that get data method out of the message inside of our listener we
should get back something that looks like this right here.
Do you remember that all information in that streaming is exchanged as strings for us.
And so we threw this object in as Jason into that event.
And so if we tried to extract information back inside of our listener from this message we're gonna
get that same object.
But in Jason form which just means we're going to have to run a Jason pass on it and the other thing
I want to point out is that they get data method is going to return either a string or a buffer.
So as far as typescript is concerned teacher wants us to make sure that we're actually working with
the string.
When we call get data.
OK.
So with all this in mind let's go back to our listener inside of our little callback right there.
I'm going to remove that console log I'm going to pull our data out of the message and so again if we
now hover over the thing it's going to tell us that it's either a string or a buffer.
We have to put it a little tight check to make sure we're actually working with the string.
We're not really going to ever be worrying or dealing with buffers for us.
It's just about always going to be a string.
We're just putting in this check right here to make typescript happy they'll say if type of data is
a string.
Now let's go ahead and try to pass it and console log out some information.
So I gonna do a console log I'm going to put a template string inside of here and I'll console log received
event number and I'm going to try to print out the number of this event or its sequence number so I'll
do some string interpolation.
Inside there I'll put in NSG.
Get sequence
and then a comma right after let's say with data and then I'll try to pass the data from the Jason that
it currently is.
So I'll do a another bit of string interpolation.
And Jason does pass data I'm going to save this just so it wraps that string around.
There we go.
All right.
So let's make sure we say this.
We're then going to go back over to a terminal and tested out so as soon as our listener restarts.
Well we have not yet received an event if we want to test this thing.
We need to publish another event.
So we'll go back over the publisher window but in the RSS hit enter and now we'll see right there received
event 18 with data.
And unfortunately our data was turned into object object just an oddity of console logging with no J.S.
rather than passing the Jason out.
Let's just leave it as a string so we can make sure that the raw data is inside there.
So back at this console log I will remove the Jason pass and just console log the data itself I'll say
this go back over I'm going to emit another event by doing another R S on the left.
And that's better.
So now I see it received event number 19 with that data.
If I start to publish a couple more events by restarting at the publisher you're going to see that that
sequence number gets incremented every single time so event number 19 20 21 22 23 and so on.
Go.
All right.
So just a little bit more of terminology and how we work with the incoming message.
I did promise at the end of last video that things were about to get all that crazy doesn't seem like
it yet.
Right.
Okay well just give me another video or two so quick pause.
I'll see you in just a minute.