We've now spent a pretty good amount of time learning about gnats and I would say that you pretty much
know the basics of what you need to know to really make use of it inside of some application.
Now at this point I can't take a look at the code that we've written now inside this listener file and
the publisher file and I just want to point out that there is a lot of boilerplate inside of here to
just get to the point where we can actually receive a message and do something with it.
So in total we essentially a forty two lines of code.
We're only just about one line.
Really just this right here has some logic related to really running some information or processing
some incoming event.
This line really is the only one where we receive some event data and do something with it.
So it took us 41 lines to get one line of essentially business logic put together.
Now eventually we're going to have a lot of listeners inside of application.
There are going to be many different kinds of events we have to listen to.
And as you can imagine we do not want to write out this entire 41 lines of setup for every different
event that we're going to try to deal with.
So with that in mind starting this video we're going to try to refactor a lot of this logic around publishing
and receiving messages to make it a lot easier to publish and receive.
We're going to write out an initial implementation to make all this stuff a lot easier.
Inside of this test project that we're currently working on and eventually we're going to move all this
logic over to our common module so we can use it across all of our different services let's first begin
by getting a better idea of exactly how we're going to make this stuff more usable in nature.
So we're going to first begin on the listener side of things.
We're going to create a class called listener.
This is going to be an abstract class.
Remember abstract classes are not meant to be used directly.
Instead we're going to subclass them in order to use them.
I'll show you more on what that means in just a moment.
Right now let's just take a look at the properties are going to define inside this class.
The entire goal the class is to essentially automate as much of all this stuff as possible so inside
this class we're going to have a property called subject.
There's gonna be a string that's gonna be the name of the channel that we're going to listen to.
We're going to have an on message function.
This is where we're going to actually run our business logic or implement the business logic we're going
to make sure that our class listener receives a stay on client to use to listen for incoming events
and whatnot.
We're gonna make sure this thing has a Q group name.
We're going to make sure that it has an equate property.
We discussed the goal of this manual act mode right here and as we saw there was that automatic 30 second
time out but it turns out that when we create the subscription we can also customize that 30 seconds.
So we might for some listeners want to customize that equate down to maybe five seconds or so.
So we'll make sure that we add in some properties just to allow ourselves to customize that window of
how long we have to acknowledge an incoming message we'll make a method that's going to make it a lot
easier to create these options right here.
So essentially just wrap up those lines of code.
We'll have a listen method which is where we're going to actually set up our subscription.
And then finally a helper method that will call pass message that is going to take a look at the incoming
message.
Figure out what kind of data is inside there and pass it in some way.
So essentially like this right here more or less where we pulled out the data and then we figured out
whether or not that data was a string or a buffer.
And eventually pass that data into actual object from the string or the Jason that is some very purposefully
just going through these properties very quickly right now.
Because once we start writing some code they will all make a lot more sense lasting on mentioned is
what we really mean by abstract class and why these two properties are labeled as abstract.
So the real idea here is that we're not going to use class of listener directly.
Instead we are going to subclass this listener into things like ticket create a listener and order updated
listener and then inside these subclasses we're going to put together some different options to make
sure that it listens specifically for say a ticket created event or in order updated event.
So in other words we're gonna customize the subject we're listening to the subclass and we're also going
to customize the on message method inside the subclass as well to make sure that when we receive some
event inside of a ticket created versus order updated we can do some business logic or do some appropriate
processing for a ticket created event or in order updated event.
So that's the general idea.
So right now really just going to focus on creating class listener will then take a look at how we can
somehow subclass it into something that's more usable.
OK.
Quick pause right here.
Let's start putting the thing together in just a moment.