All right.
Let's get started working on this event bus implementation to get started.
We're going to first create a new folder inside of our project directory to house all the code for this
event bus.
I'm back at my terminal.
I'm gonna open up yet another terminal window.
So I've now got four.
Got one for the react up one for posts one for comments.
I'm done here in the new one in the new one.
I'm gonna change back to my blog directory which is housing all of my three overall services.
Then in this folder I'm going to create a new folder called events we'll call it.
How about event bus.
Just be really clear then change into that directory let me make this when there's a little bit larger
inside of here we're gonna generate a new package not just on file.
So that's an NPM in it Dash y and then we're going to install express Node 1 access and I think that
is about it.
Yep that's it.
We'll go ahead and run that again after this is done.
I'm gonna open up my code editor inside this directory
All right.
So as usual we're going to create an indexed dot J S file inside of your at the very top.
We're going to import Express.
We're going to get body parser
and we'll get access as well.
And then going to create a new express application
then associate the body parser.
Jason middleware with this app so we'll do an app use body parser Dodd.
Jason and then that's pretty much it for the initial setup.
Now all we have to do is implement this endpoint to watch for incoming events but as we just saw in
this diagram back over here.
It's going to be a post request handler that's going to watch for a post request to slash events anytime
we receive an event or it's going to turn right back around make a series of requests to these different
servers so we'll set up a post request handler on slash events.
I'm gonna put in my callback right here.
We're going to get our event which we're going to assume is just gonna be the request body.
So whatever comes along in the request body that is going to be our event.
We don't know if it's gonna be a on object.
We don't know if it's gonna be a string.
No.
Who knows what.
All we know is that we're going to take whatever is inside that request body and send it off to all
of our different running services.
So now that we've got that event we're going to make our series of post requests to our other running
services.
So we'll do an axiom is not post to GDP all in Flash Flash local host or thousand flash events.
And the data that we're going to send along is that exact event
and then going to duplicate that line twice and I'll put in local Post four thousand one and then localized
4000 to.
And then finally at the very bottom we'll do a resort sent and we'll just say anytime someone tries
to emit an event we'll just send back something and says status.
OK just to indicate that everything worked as expected.
Now there's one downside to the implementation we have right here.
We are assuming that all three of these post requests are always going to succeed.
We don't have any handling code around the possibility of one dam failing.
That's just me I want to point out right now because it's going to start to become a little bit relevant
as we start to implement each these different event and point handlers.
So now finally at the very bottom we're gonna make sure that this thing is listening.
So we'll put on an app.
Listen we're gonna have this listen to four thousand and five.
Notice how I skipped a couple of ports right here.
So we've got our posts our comments soon to be our query service and then our event bus is just going
to go directly up to four thousand five I'll put in my call back and I'll do a console dot log listening
on four thousand five
so that is it for very simple very basic event plus implementation.
Now last thing we're gonna do we're gonna open up that package dot JS on file we're gonna update our
script inside of here so I can delete the default test 1 and replace it with the start script.
Once again we'll do node man index dot J.S.
okay.
So let's go back over George terminal.
I've got my fourth terminal window right here.
So again react those comments event bus I'm inside of the event bus and inside there I'll do an NPM
start.
And so I should see a listening on four thousand five.
Very good.
Now we've got our very simple event bus put together.
Let's take a pause right here in the next video.
We're gonna make sure that whenever someone creates a post or a comment we're going to emit an event
over to our event bus so quick pause and I'll see you in just a minute.