4.16 Doing Server Polling
The final thing we want to add to our app is the ability to fetch new chirps and users from the server regularly, to keep our application up to date.
1.Getting Started2 lessons, 03:51
2.Project Setup3 lessons, 20:58
3.Server-Side Code6 lessons, 53:00
4.The Client Side16 lessons, 2:43:53
5.Conclusion1 lesson, 00:29
4.16 Doing Server Polling
The very last thing we're going to do for our application is set up server polling. Right now, we're getting a list of all the Chirps and all the users when our user initially logs into the page. However, Chirps and users are going to be created over time. And so it would be nice if those lists could be updated as time goes on. Fortunately, because of the way we've set up everything to work with actions, this is really, really simple to do. Underneath our fetchChirps and fetchUsers functions here in our api.js file, let's create two more functions. The first is going to be startFetchingChirps. This will make our first request for Chirps and then make all subsequent requests at intervals. So we can start by saying this.fetchChirps. Pretty simple. And then underneath this, let's just return setInterval. And we will call this .fetchChirps and we will call it once every second, why don't we say. And of course you can adjust that time to whatever you'd like, but we're gonna fetch new chirps every second. So then we can do something very similar for fetching users. Although we probably won't do it quite as often. We'll say start fetching users, and then of course, we'll start by doing our initial fetch for users. And then after that, we can return set interval. And we'll do this dot fetch users and why don't we say every five seconds. For example, now that we've written these functions, we'll have to call them of course, and we'll do that in main dot js. Right now, we're calling fetchChirps and fetchUsers, so we can just change the first one here to startFetchingChirps. And we'll change the second one to startFectchingUsers. So now, we can come to the browser, and I've opened up two browser windows here. The one on the left is logged into the Android account, and the one on the right is logged into the Molly account. And you can see, over here on the left, that I have the terminal open. And you can see that once a second a got chirp action occurs and once every five seconds a got users action occurs. And we can actually see this in action updating the page, if we make a chirp. So I'll just say My new Chirp, and when I click Chirp, it shows up on my page. And notice almost immediately, it shows up over here for Molly as well. Now if Molly makes a Chirp, then you can see she gets it and less than a second later, I get it as well. Let's see if this works for users. I'll go to our list of users here, and then let me log out from Molly's side here. And let me create a brand new user. All right, so I've filled in the user form and I'm gonna click sign up. I'm logged in on the user side. And you can see shortly thereafter, the new user appears in Andrew's window over here on the left. So, there you go. Our application now pulls the server and updates almost as soon as new information is ready.