In this video we're going to start implementing class listener inside of our little test project now
between the last video on this one.
I want you to notice I added on another abstract property here of Q group name.
So that means that we're going to have to define an actual value for Q Group Name inside of our subclasses
of listener okay.
So let's go back over to our Ed inside of our listener test file.
We're going to define this listener class at the very bottom will eventually move this to its own separate
file but right now this will work just fine down here.
We're going to create an abstract class called listener.
We're going to go through all the properties and add them in one by one along with some implementation
for each.
Let's first take care of this client thing.
So we want our class listener to have a client property that should be a reference to a pre initialized
Nats client.
So it should be a type stand.
When I say pre initialized I mean to say that we should have already connected to Nats successfully
to the client we're going to pass in is essentially and be like that thing right there.
After we have successfully connected
so I'm going to assume that this thing is going to have a private property client and it is going to
be a type stand a stand right here.
As you'll recall is Nats backwards.
This is what the Nats community calls clients.
We need to import this Stan type from the node Nats library at the very top of the file so I'll find
no not streaming and I'm going to get the Stan type and then back here.
We now have an around client is just saying that we have defined as property but we never initialized
it.
Let's make sure that we defined a constructor function and we're going to make sure that the constructor
receives a client that we're going to use.
It's all defined the constructor will say this thing is going to receive a client property that is of
type stand and we're gonna take that and assign it to clients.
So this client is client.
Next up we're going to make sure that we define a Q Group name and a subject.
These are both going to be abstract properties which means they must be defined by our subclass.
So I will put in at the very top of the class abstract subject like so that's going to have type string
and abstract you group name that will be a string as well.
Those are easy let's now take care of equate.
So this will be the number of seconds we have to listen to a message and act it before the event.
Eventually times out so I'm going to define this as private equate and let's actually make it protected
instead which means that the subclass can define it if it wants to go into default is to be five seconds
so five thousand milliseconds five seconds.
Next up subscription options are subscription options and total are gonna be very similar to the options
we had written out up here.
We're just to find them inside of a helper method.
Just have one convenient location for them.
So define subscription options inside of here we go.
We will return this stock client no client is our Nats library or Nats instance not subscription options
you're gonna chain on a couple of different options to this thing versus say set deliver all available
remember that's gonna make sure that the very first time our subscription is created we get all messages
on this channel that have ever been emitted well then set manual act mode to true.
We're gonna set that equate to this not equate.
So that's how we customize the acknowledgement time out period normally 30 seconds.
But again we're going to certain that down to five seconds and then finally we'll also set up our durable
name set durable name for the durable name.
We're just going to end up using the same Q Group Name so we're going to use the Q Group Name both as
the Q Group Name and as the Journal name because it usually makes a lot of sense to just use the same
one.
It's very rare that we're going to distinctly want these things to be different.
So for set terrible name we will pass in this dot Q Group Name
after that.
Let's put together our listen method to the goal of listen is to really just set up the subscription
itself.
Whenever we get an incoming message we're going to pass the message using a pass message method and
then eventually call an on message method so inside of a listen right here we're going to first create
the subscription itself so cons subscription is this client subscribe and we're gonna pass in those
three arguments the first one is going to be the subject that we want to listen to.
So this not subject
the second is going to be our Q Group name
and then third will be our actual subscription options.
So we'll call this dot subscription options
that will give us our subscription after we get it.
We have to start to listen to it.
We'll stay on message.
We're going to receive MSP which is of type message inside.
We'll do just a little bit of logging info just to say hey we just received a message so that we can
more easily see when we receive messages and they're being dealt with inside of our app.
So do a console log I'm gonna put a little new line inside of here because the log that we're gonna
do is a little bit long.
I'm gonna put in a template string so back ticks and we'll say Message received we're going to print
out the subject of the message that we just got and maybe we should also print out the Q Group name
for this thing as well just we understand exactly what group of services receive this message.
So I will also print out this dot Q Group Name
after that.
We're going to want to pass the incoming message and pull the data out of it.
So I'm going to really quickly define R's message down here.
So remember when we get a message we have to call that get data method on it.
That's gonna give us either a buffer or a string.
We're going to make sure that if it is a string we just person data out of it and return the results
of that so we're going to assume that we're going to receive a message in this thing of message.
We'll get our data out.
So message data
then if we are working with the string or a buffer remember it can be either we mouse over data right
here we'll handle it appropriately we're gonna use an attorney or expression to do this.
So we'll say return type of data equal to string.
So if it is a string I want to do either one thing or the other.
So just a normal ternary expression.
So if it is a string let's go ahead and do a J signed up pass of data otherwise if it is a buffer we'll
handle it appropriately.
Here's how we would handle a buffer.
We don't ever expect to get a buffer but we're gonna write some code to handle it just in case we'll
do a Jason pass data dot to string UTF 8.
That's how we would pass a buffer and get some Jason out of it.
So back inside of this subscription dot on we will call or get past data and that's going to come from
this dot pass message and we'll pass on the message or excuse me MSE and then finally we are going to
assume that there is some kind of on message event or me on message method that we want to call with
this event data that we just passed out of it.
So I'm going to try to call this dot on message with the pass data and just for consistency sake we
don't really know what we want to do inside of.
On message just yet.
So in addition to the actual data that we're gonna throw in there let's just pass in the entire message
the original one as well just in case we need to get access to some other property off the message itself.
So it's and message like so we are getting an air out of on message.
So we do have to define that as an abstract method on our class.
And again by being an abstract method that means we must define it inside of a child or subclass.
Well very quickly define on message at the very top by all of our other properties about by all the
other abstract ones.
So we'll say abstract on message and we're going to assume that this is going to take in some data right
now we'll say that it's of type any and a message of type message and it's going to return void okay.
So that is our class listener lot of code inside of here.
But really all we did was kind of massage a lot of the stuff that we already wrote out.
This is all the same stuff we already put together up here.
We just kind of redistribute it around.
Let's take a pause right here.
When we come back next video we're going to take a look at how we can now subclass this thing and very
easily create a listener.