We have connected to our Net Server successfully so we're now going to attempt to publish some data
to it.
Let me first tell you a quick diagram just to introduce a couple of different pieces of terminology
on how we're going to publish data.
OK.
So in this diagram on the left hand side we've got our publisher and a listener.
We have not yet put the listener together but I want to give you a brief description of what we're going
to eventually do inside there.
Then on the right hand side is our Nat string server.
And inside there is some list of channels.
Remember in the Nats world we always publish information to a very specific channel.
Here's how we actually share information around inside of our publisher.
We're gonna come up with some piece of data that we want to share with everything else that's listening
to the stats server.
So we're going to create some object or something like that.
We're then also gonna come up with a subject a subject is the name of the channel that we want to share
this information with you and I we're going to imagine that we're trying to share some data about a
ticket that was just created inside of our application.
So our data is going to be essentially a ticket.
It will be something it has an IP a title and a price are subject or the name of the channel that we
want to share this information with.
We're gonna give it a name that kind of describes the type of data that we're trying to share.
So in our case we are trying to share some data related to the creation of a ticket.
The name of our channel will be ticket colon created.
We're gonna take this data and these subjects and pass it in to the stand client the stand client is
then going to reach over to the Nat string server when it reaches over.
It's going to say hey we want to share some information specifically this data over the channel or the
subject name of ticket created the Nat string server is then going to add ticket colon created to its
overall list of channels.
It'll then take our data and broadcast it out of that channel to anyone who's listening now at present
inside our application.
We don't really have any implementation for the listener at all.
So if we tried to share data or published some data on this channel right now that server will say OK
I will try to do this but just so you know no one's really listening.
So we can always attempt to publish data and if no one's listening.
Well that's pretty much it.
Our data is just going to be emitted into the void and no one's really going to hear from us about whatever
just occurred so at some point time we will create this listener when we make the listener.
We're going to have a subject that we want to listen to.
So in our case the subject we want to listen to are the channel we want to listen to will be ticket
created so we'll take ticket created and pass it also into the stand client that's going to tell this
damn client to tell the NAT server that anytime some information is published from this channel we want
to receive a copy of that information and we'll receive that information in something called the subscription.
So a subscription is what is going to actually listen for some information and tell us whenever some
information is received again.
So it's kind of a high level overview.
And mostly I just want to show you some different important pieces of terminology.
So the subject is the name of the channel we want to publish information to the channel is something
that we listen to the subscription is what's going to actually listen to the channel and eventually
it receives some data let's now go back over to our publisher T.S. file and we're going to try to publish
an event back inside a publisher T.S. right after our console log.
I'm going to write out the data object.
So this will be the information we want to share.
We're going to again kind of pretend that we're sharing information about a ticket being created.
So we'll give this thing some I.D. a title of concerts and maybe a price of 20.
Now there is a little gotcha around that's in the gotcha is that we can only essentially share strings
or essentially raw data.
What we have right here is an object that we want to share.
So we can not share directly a plain javascript object in order to share this or send over to that string
server.
We first have to convert it into Jason which is a plain string.
So essentially all of our data before we send over to that streaming server we just have to convert
it into Jason.
So I got to wrap that with Jason trying to find like some then after that we'll call the Stan publish
function the publish function is how we actually publish some data when we call publish.
We're going to pass in the name of the channel or the subject we want to publish this information too.
And we'll also pass in the data we want to share as well.
So we'll call Stan dot publish the first argument is gonna be the subject name.
So in our case that is ticket calling created the second argument is the data we want to share.
There's then an optional third argument.
So we do not have to do this.
We can't if we want to.
The third argument is a callback function.
This function is going to be invoked after we publish this data.
So inside of here we might just put in a simple console log and say event published
let's now save this file just so you know the.
Yes no Dev program that we're using inside of our package based on script is going to automatically
rerun the publisher test file as soon as we save it.
So as soon as we save this we should see that we reconnect to our Nats cluster or Nat server and then
attempt to publish some information.
So going to say this go backward my terminal.
And sure enough I see I was sure connected to Nats and then even published.
All right.
Well that's the basics of how we publish events.
They're just you know in the world of Nats rather than referring to this data that we send along as
a event or something like that.
It is more commonly referred to as a message.
So you will see a lot of stuff in the Nats documentation referring to this data where this information
was sent out as a message whereas in this course referring to it as an event because that's really what
it is to us it's an event it's something that occurred inside of our app that we want to share with
other services.
Now we've got our publisher put together with a very basic implementation.
We're going to take a pause and then tackle our listener in the next video.