Enough lecturing.
Time to start writing out some code.
So here's our short term goal.
Here's where we're going to go through the next couple of videos we're gonna create a new sub project
inside of our current project directory.
We're going to make sure that it has types of script support because I want to make sure that we understand
how to work with no not streaming while using typescript.
We're then going to install no net streaming into it and we're going to use it to connect toward that
streaming server then inside this little sub project we're going to make sure that we have two different
NPM scripts essentially two different ways to run our project.
One script is going to try to run some code to emit events.
Internet streaming server and the other script is going to run some code that's going to listen for
events so we'll then have essentially two separate programs where we can emit events or listen for events
last important note that won't throw out is that this program or essentially is two separate programs
are going to be executed from outside of Cuban settings.
So we're not going to worry about creating a deployment or anything like that.
However this is going to present some interesting challenge because we're going to want to connect to
the node that streams server or the net streaming server that is running inside of our Cuban cities
cluster.
So we just need to keep in mind that we need to somehow get access to the Nat streaming server.
So let's get to it to get started.
I can go back over to my terminal.
I'm inside of my route ticketing directory inside of you're going to make a folder called Nat's test
the only reason I'm calling it Dash test is to just make it really clear that this is some little test
project inside of your going to change into that directory generate a package not just on file
will then install a couple of different dependencies.
So in total we're going to get that node and that's streaming thing.
We're also going to get a couple of other dependencies just to run this project using typescript so
npm install we're going to get node Nats streaming T.S. node Dev TypeScript and we're also gonna get
a tight definition file for node J.S. itself at types flash node.
Let's run that it's gonna take just a moment or two to install all that stuff.
So while that's running let's go back over to our Ed and find the new Nats dash test directory inside
they're going to make a new folder called as RC and inside of SLC I'm going to create two files.
I'm going to call one publisher that T.S. and the other listener got to yes so these are going to be
essentially the two separate programs we're going to put together.
One is going to be dedicated to listening for events as it guess that's listener dot to us and the other
is going to be dedicated to publishing events.
Obviously publisher T.S. before worrying about writing out any code inside of here.
Let's write out some different scripts to run each of these two separate files inside a package.
Jason.
I will delete the test script and I'm going to replace it with publish
whenever we run the published commands.
We're going to execute T.S. node Dev.
We're gonna put in a option here of dash dash notify false and then we're going to try to run the SDR
see publisher dot t s file then as a second script we're going to do pretty much the same thing.
It will change the script name to listen.
Still gonna do.
Yes.
No Dev dash dash notify false our.
And this time around listener dot.
Yes
yeah that looks good.
So we've got some root files we've got dependencies.
We've got our scripts before we start to write out some code.
One last little administrative thing we have to do.
We are making use of typescript.
So we really should still create a T S config file inside of here to do so we can't go back over to
our terminal and again inside of our Nat stash test directory a TSC dash dash in it.
And of course this command is assuming that you have typescript globally installed on your system.
So if you get a an error from that you'll need to do an npm install DST type TypeScript and then rerun
TSC dash dash in it OK.
So let's go back over.
We're gonna first start off inside of our publisher dot t yes file inside of you're at the very top.
Let's first import the node Nat streaming library.
So we're going to import nets from node Nats streaming we're then going to use this library to create
a client.
The client is what is going to actually connect to our Nat streaming server and tried to exchange some
information with its will say client is Nats dot connect.
Now quick note here you're going to see inside of just about all the documentation rather than calling
this thing client which is a very reasonable term for it.
Instead a lot of documentation is gonna refer to this as Stan.
So Stan is just kind of a nat streaming server or Nats project little piece of terminology is just what
they like to call a client Nats as you'd kind of guess is simply to me or Stan is just Nats backwards.
So Nats is the library we're kind of a class of sorts.
Stan is an actual instance or a client that we're using to connect to Nat streaming server if you want
to instead called his client because it's a thousand times more clear.
That's totally fine.
But I'm going to stick with the community convention which is to call it Stan it's now inside of connect
right here we're getting an air of course.
So we need to put in a couple of different arguments to this thing.
The first two arguments are gonna put in where it's going to throw in four right now and we'll come
back and discuss why we use these values.
And just a little bit.
The first thing I'm gonna put in is a string of ticketing but a second argument I'm gonna put an ABC
and then finally the last argument I'm gonna put in an object to specify a URL and I'll put it in a
address right here of H TTP and slash slash local post or to do to
then after that we're going to wait.
Or Stan or the client in this case to successfully connect to our Nat streaming server.
So to wait for this thing to actually connect.
Unfortunately again we don't get to make use of that nice async await syntax.
Instead we have to take a primarily event driven approach.
So after the client successfully connects to the Natsumi server is going to emit a Connect event.
So we're going to listen for the Connect event.
I'm going to put in Stan dot on next and then a function as a second argument.
So this function will be executed after the client has successfully connected to the Nat streaming server.
Let's do a console log inside of here just to confirm that that takes place.
So do a console log of publisher connected to that's it.
So this looks good.
Let's save this right here.
If we try to run this code right now we're going to get an error because our net streaming server is
not available on localized or two to two remember answering server is running inside of our communities
cluster and by default we don't have direct access to anything running inside there.
So let's take a pause right here.
We'll come back next video and figure out how we can somehow get access to the Nat streaming server
very easily just for test purposes.