In this video we're going to try implementing option number three inside of our application.
So we're going to take our query service down.
We're then going to try to create some number of posts and some number of comments as well.
We're then going to launch our query service and we're going to try to make sure that the query service
can reach out to the event boss until it give me all the events that occurred up to this point in time.
Now I want to remind you that we are putting together a homegrown version of an event bus.
So there are real open source projects very production quality in nature that are going to do a lot
of what we are discussing right here and a much more automated and well-intentioned fashion that would
then what we are doing currently.
So all you and I are doing are trying to get a sense of what is going on here.
You should not use this code or about to write in a production environment.
I just want you to have an idea of what is going on behind the scenes.
The first thing we're going to do is to make sure that whenever our event bus receives an event we are
going to store it inside of an array.
We're then going to add in a new endpoint to our event bus an endpoint that can allow us to retrieve
all the events that have ever occurred.
Well then make sure that whenever we launch the query service we'll have it reach out to the event bus
and request all the events that have occurred and we'll make sure that the query service attempts to
process all that data.
Let's get to it.
The first thing we're gonna do is open up our event bus and make sure that we store all the different
events that we collect over time so back inside my editor I'll find the event bus implementation series
index dot J.S. Right above our post request handler where we deal with incoming events.
I'm going to simply create a new variable called events and I'll have it be an array like so.
So it's going to take any every single incoming event and throw it into this array then inside of our
post cluster handler.
We'll take a look at the event that we receive and we will push it into that array.
We'll do it and events that push event now critically the most recent event is gonna be at the end of
this array.
The first event to receive will be at the very front of the array we're then going to create another
endpoint on this event bus to retrieve all the events that have ever occurred.
So after that existing post request handler I'll put in an app dot get or events with req and rez and
then if anyone ever makes a request to slash events right here we will simply respond with that big
list of events.
Now again one repeat what I said just two minutes ago.
Real implementations of event buses are way more complex than what we have right here.
This is just to give you a sense of this approach of trying to synchronize services after they've been
out of service down not even a known existence or whatever else OK.
So now let's go over to our query service.
I'm going to close that file.
I'll go over to the query service and open up the index dot J.S. file.
The first thing we're gonna do inside of here is extract some of the logic that we have inside of our
post events.
Root handler right now instead of here we've got a lot of code that knows exactly how to deal with or
handle any given event that's all localized directly inside this post request handler.
Right now but we're going to need to very quickly process events in some different locations inside
this file.
Long story short we're going to take all these statements and extract them into a separate helper function
inside this post.
Events are going to find the first if statement.
I'm going to go down to the bottom if statement right here.
I'm going to cut all that and so I'm just left with type data a console log in the send right there
I'm then going to define a new function right underneath posts and I'll call it handle event
and then I'm going to paste all the stuff that we just cut inside of that function
now this function is going to need access to the data of the event and the type as well.
So I'm going to assume that we're going to provide the type and the data as arguments to handle event
then finally back inside of our post your quest handler is right here going to delete that console log
which we don't really need anymore and I'll replace it with a call to handle event and I will pass in
the type and data so all we just did is extract all that you've been handling data into a reusable function
OK.
So now let's say we need to do whenever our query service comes online and it starts listening on port
four thousand into right after that would probably be a pretty good time to make a request over to our
event bus and try to get a listing of all the different events that have been emitted up to this point
in time so right after we do that console log I'm going to add in and await CEOs dot get we're going
to reach out to each GDP call and slash slash local host four thousand five flash events that's gonna
give us all the events that have occurred over time.
Remember we're making a get request right here though assign that to some red variable I'm gonna make
sure I also mark the enclosing function as a sync and then finally we're going to iterate through all
the events that have occurred using a simple four of loop.
We can also do a for each whatever you want to do.
But in this case let's use for a very simple straightforward so say let event of red dots data.
Remember whenever we use axis the response or the actual data we get back is available on the data property
of this response so then inside of here maybe first we could do a quick console log and say something
like processing event and then event type and then right after that we'll go ahead and actually handle
the event.
So handle event we'll put in the event type in the event data and that should be at okay.
So let's save this.
This is kind of a long video so take a quick pause right here and then test it out in just a moment.