Take a tour through a rock, paper, scissors game implemented in Node.js and see how quickly performance and stability problems appear.
- [Instructor] The application we'll be working on is an implementation of the game, rock, paper, scissors. What's the game? It's a simple two-player hand-gesture game that can be played in seconds. There are five basic rules. Rock breaks scissors, scissors cuts paper, and paper covers rock. A tie is replayed. In real life the players reveal their choice at the same time. As an example, player one chooses rock and player two chooses scissors.
Rock breaks scissors and therefor player one is the winner. In our web version, visiting the site creates a player that is stored in the session. A player can create and join a game. Once joined, a player can make their choice, rock, paper or scissors. When both players make a choice the game is judged and the winner is revealed. To build this application a few technologies were used. Node.js or course is the programming language.
Express is used as the web application framework. On the front end, Pug provided a simple web template and Bootstrap as a framework. Finally, MySQL is used for persistent storage. To keep things focused I avoided some technologies that are commonly used in more robust applications. For example there's no Ajax, this is just a simple HTML form based application. There are no real time libraries like Socket.IO because it's literally a course in itself.
In the meantime, sit back for a moment and I'll take you through a brief tour of what we'll be working on. This is a browser based game. The home page lists both finished and available games. At this time there aren't any. Players are created at the same time as the session so all I need to do to start a new game is to click Start new. I have three choices, Rock, Paper or Scissors. I'm gonna choose Rock, so I'll click it. The page reloads and I've chosen rock.
Since players are stored in the session I'm gonna open a guest instance and access the same page. So I'll just copy the URL and go to New Incognito Window, and paste. The other player cannot see their opponent's choice. I'd like to play, so I'll click Join. I have the same choices, I'm going to choose Paper because paper covers rock. Excellent, I've succeeded in beating myself.
Ready to break the game? It won't be hard, let's try to go to the game Fish. Well, it just hangs, that's not good. I'm gonna give up and just go Home. Let's start a new game by clicking Start new. This time I'm gonna start with a form by editing an element and changing the value to be fish instead of rock.
So the value is going to be fish, and we've update the label. We'll click Fish, oh oo, it hung again. If we wait long enough we'll get an error message from the browser. We've got some pretty obvious problems here. I can successfully play a game but the application hangs if I give it invalid input. How can we find and fix the problems? We've only scratched the surface of the problems lurking beneath this innocent little game.
Let's get started with Node.js to bugging and performance tuning.
- Building a troubleshooting mindset
- Why measure performance?
- What's a microservice architecture?
- Managing microservices with PM2
- Effective logging strategies
- Debugging Node.js applications
- Benchmarking performance
- Profiling code execution
- Knowing what to optimize