I'm inside of our publisher t s file inside of here.
We are creating some piece of data and then publishing it off to the Internet server.
Now when we take a look at this published code it definitely works right now.
But I want you to really imagine all the locations where we could very easily make a typo and not really
have anything to catch that typo.
If I take a look at this data object I could very easily accidentally define price as a string like
Dollar Sign 20 instead of Title I could very accidentally put in name on the subject of ticket created
right here.
I could very accidentally put in ticket created which is a different channel.
As far as Napster misery is concerned.
So even though this code is very simple right now and easy to understand there's an incredible number
of locations where we could very easily make a typo that would be rather hard to catch when we start
to deploy a service so in this video we're gonna start to put together a publisher bass class very similar
to the base listener class we already put together the entire goal of all this is to get typescript
to check our code and make sure that whenever we tried to emit some kind of event or some data it has
the correct structure the correct properties the correct types and make sure that whenever we actually
publish that data we provide a valid subject name inside of here as well.
That is the goal of all the stuff we are trying to get typescript to help us out and make sure we don't
make these silly typos.
At this point just as a side comment this all might seem really unnecessary.
You might be thinking Steven when am I going to make a typo.
Well remember on a micro services project the kind of underlying assumption here is that you're going
to be working on a micro services project with a team and so you need something in place to make sure
that your teammates do not make mistakes just as much as you need to make sure that you are not going
to make a mistake.
It is very easy for people in a team to just very slightly mis communicate on whether some property
name is title or name.
You never know.
So long story short we're gonna write out some code that looks very very similar almost identical to
the base listener class.
It's gonna be an abstract class.
We're going to try to have some coupling or checking using a subject and some other kind of argument
thingamajig or something like that.
Let's just get to it and start writing some code inside of my events directory.
I'll make a new file called base publisher dot.
Yes inside of here I'm going to immediately add in a couple of imports.
I'm gonna get Stan from node Nat streaming and I'm going to get our big list of subjects from subjects
and then going to define and export an abstract class called publisher I want us to be a generic class
because this is how we're going to eventually enforce some type checking on the subject for all the
different subclasses a publisher we put together.
So this is going to be a generic class we're going to accept some type that we're going to refer to
as t and it's going to extend event let's define that event interface right above so in order to be
an event you must have a subject and a subject must be one of the subjects listed out inside that subjects.
Enum you must also have a data property and type can be any.
We don't really want to enforce any specific data type around this based interface now inside a publisher.
We're going to do something very similar that we did inside of our listener where we're gonna say that
this thing has a couple of different abstract properties so we're not gonna use this publisher directly
same way we did not use the listener directly.
We are going to subclass publisher and have different publishers that are meant to publish different
kinds of events so we're going to define an abstract property of subject and this must be equal to the
custom type right here or the generic types subject property our publisher is also going to need a copy
of the Nats client that hopefully has already been initialized inside our application so we'll make
sure this thing has a constructor where we're going to receive some client of type Nats.
I'll make sure that I update the client property and I'll make sure sorry not that Stan is such an easy
thing to forget who came up with that they should really show is called a client but I digress.
Also make sure that our class has a property of Klein as well.
We'll say client will be of type Stan.
I'm gonna make sure this thing is marked as private just so no one else tries to reach in and mess around
with it at all OK.
So now we need a function that is going to actually start to publish some information just as we had
the listen function inside of our base listener class.
No listen right here.
This is what actually reached out with the Stan client and created a subscription.
We want to have a function very similar to that inside of our publisher.
And the goal this function is going to be to take some data or like an event data and publish it off
to the Nat streaming server.
So we're going to define a function called publish this thing is gonna take some data that we want to
publish and its type is going to be up type T data
then inside of here we're going to make use of our Nat's clients very similar to how we did back over
here inside of our publisher t s file.
We're going to call publish on that client.
We're going to provide the subject that we want to provide this or publish this data as a then the data
as a second argument.
So inside of here we'll say this stock client publish
the subject that we want to publish this event too.
It's going to be the subject property.
So this subject and the data that we want to be publishing is the data argument data.
So now there is an optional third argument here of a callback function.
We're going to come back and work with this callback function in just a little bit.
But right now whenever it gets executed after we publish this event let's just do a console log of event
published.
And like I said it will eventually come back and do something a little bit more with the callback.
All right.
So believe it or not that is pretty much it for this abstract publisher base class.
It's now just the same as what we're doing with our listener.
This is the definition of what a publisher is.
We're now going to subclass the publisher into something that can actually publish very specific kinds
of events such as a ticket created event so inside of my events directory I'm going to make a new file
called Ticket created publisher T.S. inside of here at the very top we will import the publisher that
we just created from base publisher I will also import yet ticket created events from ticket created
events.
And then finally I'm going to get our subjects into
from subjects that inside of your we're going to export a class called Ticket created publisher and
it's going to extend publisher and because we are making use a publisher remember a publisher is a generic
class which means we have to provide a type arguments.
So the type of event that we're trying to publish here is a ticket created event.
So we'll stick and ticket redid events like so
then we're going to define the subject.
If we hover over this as a matter of fact it'll tell us we have to define the subject very similar to
what we did inside the listener.
We're going to say the subject must always be of type subjects dot ticket created and the actual value
from it must be subjects to get created and that's pretty much it.
We do not have to define a publish function here we already define the Polish function inside the publisher
based class.
So how would we actually make use of this.
Well this video is running a bit long.
Let's take a quick pause right here.
We'll come back the next video and then see how we actually make use of this custom.
Publisher.