Instructor: In the last section,
we started talking about the overall architecture of the app
that we're going to put together.
Again, I can't say it enough,
way more complicated than it needs to be,
just so we have multiple containers to work with.
So in this section,
we're gonna start putting together the source code
for the entire application.
A quick note here in this section
and the next couple of videos,
we're going to be putting together
the JavaScript side of the application.
We're not gonna touch any Docker stuff for right now.
So no Dockerfiles, no Docker Compose,
no Travis, no deployment,
nothing like that.
The reason that we're gonna focus just
on the JavaScript side for right now,
is that if you do not care about all this JavaScript stuff
like you don't care about React,
you don't care about Express,
you don't care about any of that stuff,
totally fine.
You can skip this video
and the next couple,
you can move on to the next section.
And in that section,
I'm gonna post all the code
that we're gonna write right now up for download.
So if you don't care about JavaScript stuff at all,
totally fine.
Pause the video here,
and skip ahead a couple of videos.
I wanna make sure that if you are not a JavaScript person,
you don't have to sit through all this stuff.
I'm only showing how to put
the application together from scratch
in case you are a JavaScript person
and you're kinda curious about how to work with Redis
and set up this kind of like,
worker over here,
and Postgres and Redis,
and all that kind of other good stuff.
So again, if you don't care about the JavaScript stuff,
pause here, skip forward a couple videos.
Otherwise, we're gonna get started right now.
Okay, so the first thing we're gonna put together
is this worker process.
Remember, the worker process is going to watch Redis
for any new indexes that get inserted.
Anytime the worker sees a new index added to Redis
it's gonna pull that index
and then calculate the appropriate Fibonacci value for it.
Now, as we put this worker together,
I'm not gonna tell you too much about
the behind the scenes stuff,
like specifically how we work with Redis
from JavaScript code,
because that's not really the goal of this course.
But I will give you just a little bit of commentary
to make sure you have the general idea of what's going on.
So let's get to it.
I'm gonna first begin by changing on over to my terminal.
You'll notice that I'm back
in a workspace directory of sorts,
so I'm no longer inside of any of the folders
that we've been working on so far.
I'm gonna make a new folder inside
of here called, "Complex."
And the idea behind this
is that the name of our application
will be kind of like a more complex application.
I'm then going to change into that folder
and then inside of here
I'm gonna make another folder
that's gonna house all of the source code
for this kind of worker process
that's gonna calculate the actual Fibonacci values.
So I'll make a new folder inside of here called, "Worker."
And then finally,
I'm gonna start up my code editor inside
of the complex directory.
All right.
So here's the worker folder that we just created.
I'm gonna first begin by adding
a package.json file to it.
So inside the worker directory,
I'll make a package.json file.
Then inside of here,
we'll put down a little bit of configuration.
We're gonna add in a couple of dependencies
that we're going to need.
The first dependency that I'm going to want is nodemon,
which is gonna give us some live reload of code anytime
that we change the source of our project.
We'll go more into that a little bit later.
The version of nodemon that I'm gonna get is 1.18.3.
I'm also gonna get a Redis client
that will allow us to connect to a copy of Redis
and pull values out of it
and do all the stuff that we need to do with Redis.
And for that, I'm gonna get version 2.8.0, like so.
Now, at this point,
please make sure that you're keeping track
of the code that I'm typing
and the code you're typing.
Please double-check for typos and whatnot
because we're not gonna run any of this code
for several sections.
So if you make a typo in here,
it's gonna be kinda challenging to come back later on
and find where you made that typo.
So for example, make sure you get the comma
at the end of the nodemon line,
make sure you do not have a comma
at the end of the Redis sign,
and so on.
So now we're going to also put a script section
in here as well.
I'm gonna add a comma at the end of dependencies,
and I'll put in scripts.
And then we're gonna add on a start script
of node index.js,
and I'll do a dev script of simply,
nodemon, like so.
All right, so that's it for our package.json file.
Again, please double-check the placement of your commas.
Please make sure
that you're using double quotes everywhere inside of here,
and both all the keys
and all the values should have double quotes
around them as well.
So that's all we need for package.json.
I'm now gonna create a separate file inside
of here called, "index.js."
So this is where we're gonna put all of our primary logic
for connecting to Redis,
watching for values,
and then eventually, calculating our Fibonacci value.
All the configuration or kind of keys
that we're going to need for connecting over to Redis
are gonna be stored inside of a separate file.
So the first thing I'm gonna do inside of here
is require in a file that we're going to call, "Keys."
And again, this file is gonna house the host name
and the port required for connecting over to Redis.
So inside of the worker folder,
I'm gonna make another file called, "Keys.js,"
and inside of here,
we're gonna add in a module.exports.
That's gonna be an object,
that's gonna have a Redis host of process.env Redis host,
and a Redis port of process.env Redis port.
So anytime that we want to connect to Redis,
we're gonna look for the host name
or essentially, URL of sorts, for Redis,
and the port that we're supposed to connect to it from,
from our environment variables.
So when we run this application,
we darn sure better well have
a Redis host environment variable
and a Redis port environment variable defined as well.
So that's all we have to put inside the keys file.
I'm gonna go back over to index.js
and then inside of here,
now that we've got our API keys,
or essentially, just connection keys,
I should call it, put together.
We're gonna start putting together the logic
to get a connection over to our Redis server.
So I'm gonna first import a Redis client,
we'll then create a Redis client.
And to this thing,
we're gonna pass an object
that has a host of keys.redis host,
a port of keys.redis port,
and we're gonna add on one other little option
in here called, "Retry underscore strategy."
And this is gonna tell the Redis client
that we're using right here,
that if it ever loses connection to our Redis server,
it should attempt to automatically reconnect
to the server once every one second
or once every 1,000 milliseconds.
After that, we're going to make a duplicate
of the Redis client that we just put together.
We'll talk about why we're doing that in just a moment.
We'll say, consSub is redisclient.duplicate, like so.
All right, after that,
we'll then put together the function
that's going to be actually used
for calculating these Fibonacci values,
given some particular index.
So the work function,
we'll say, function fib will be called with some index.
We're gonna say, "If index is ever less than two,
then we're going to return the value one,
otherwise, we're going to return fib index minus one
plus fib index minus two.
This right here is a very classic solution
to the Fibonacci sequence.
It's going to return the value one
if the index submitted is ever less than two.
Otherwise, it's gonna take the previous two values
add them together, and then return that.
Now, we could talk for days
about exactly how this solution right here works.
If you want to, you could always go look at a blog post
or two to get a better idea about
the Fibonacci recursive solution.
Now, we're very purposefully using
a recursive solution right here
because it's very slow
and it's gonna give us kind of a better reason
for making use of Redis
and having a separate worker process
to calculate these Fibonacci values.
So this is not an ideal solution right here,
but it's one that kinda simulates why we would want to have
a separate worker process.
Now, the last thing that we need to do inside of here,
we're going to use the Redis client connection
we just put together.
We're gonna watch Redis
for any time we get a new value inserted into it.
And anytime we see a new value,
we're gonna run our fib function that we just put together.
We'll say, oops,
we'll say, sub,
which remember, is the duplicate client right here.
Sub stands for subscription
'cause we're gonna watch Redis,
and get a message anytime that a new value shows up.
We'll say, sub.on message.
So anytime that we get a new message,
run this callback function
that we're gonna add right here.
The callback function will be called
with something called a channel
and a message.
We'll then say, redisclient.hsetvalues message.
And then finally,
we're gonna calculate the actual Fibonacci value
by saying fib, parseInt,
and message, like so.
And let me zoom out so you can see that whole line.
So anytime that we get a new value that shows up in Redis,
we're going to calculate a new Fibonacci value
and then insert that into a hash of values
or a hash called, "Values."
The key will be the index that we receive.
So message right here is going to be the index value
that was submitted into our form
and then we push in the value for Fibonacci sequence
that we just calculated as well.
And then the last thing
that we're gonna do inside this file,
we'll say, sub.subscribe
to any insert event.
So anytime someone inserts a new value into Redis,
we're gonna get that value,
and attempt to calculate the Fibonacci value for it,
and then toss that value back into the Redis instance.
All right, so like I said,
we're going very light on detail right now,
because this is super off topic related to
or compared to the actual Docker stuff
that we're trying to talk about.
The very last thing I want to do
is I wanna try to run this index.js file.
The file's not gonna run successfully,
but it should at least tell us whether
or not you have a typo in here.
So back at my terminal,
I'm gonna change into that worker directory.
And then inside of here,
I'm gonna execute nodeindex.js.
Now if you see a message right here that says,
"Cannot find module Redis," that's good.
That's what we want to see.
If you made a typo somewhere in here,
like let's say, I have an extra colon or three
on this line right here,
and then I try to run nodeindex.js,
I'll see a different message.
I'll see something that says,
"Hey, you made a typo."
So if you see anything other than "Cannot find module Redis"
that means that you made a typo somewhere
and you need to double-check
and find that typo in your code and fix it.
All right, so that's it for the worker process.
Let's take a quick pause right here
and we'll continue in the next section.