After our last internal hackday, where I took a little a flak for building my project in PHP, it was clear that I needed to finally get around to building something in node.js. I love learning new languages but I hate configuring new toolchains and deploying new stacks.
I have a major nerd crush on meteor.js
curl https://install.meteor.com | /bin/sh
meteor create leaderboard
And open up http://localhost:3000 and there’s something running. I don’t think I’ll ever write any PHP again. Want to test it on a live server?
meteor deploy my-leaderboard-example.meteor.com
HTML & CSS
The CSS is pretty straightforward, the only interesting parts are the CSS3 tomfoolery to round corners and pulsate the colours. The HTML is just a very simple handlebar template that displays all the Services. Here’s the key section:
- Hooks the all_services template to a sorted list of Services
- Starts calling the updateCurrentState function on the server every 30 seconds (and logs errors to the console)
server/credentials.js is split out so that I don’t accidentally push my actual credentials to Github. Note: Meteor loads js in alphabetical order.
The meat of the program: client/client.js, is only ~30 lines:
- Export the getCurrentState function to the client as updateCurrentState
- Parse the json
- Iterate through each service
- Calculate some simple statistics
- Manually perform an upsert (update or insert accordingly). If this was more than a code sample, obviously we wouldn’t update services that haven’t changed.
- The framework handles updating the client to reflect any changes to the Services collection.