eurica

numbers for people.

Meteor.js sample integration with PagerDuty

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
cd leaderboard
meteor

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:
<template name="all_services">
{{#each services}}
{{> service}}
{{/each}}
</template>

Client Code

There’s a single line of code shared by the server and the client defining the Services collection (pdstats.js). The rest of client/client.js:

  • 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 Code

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
  • Issue a Meteor.http.call to the PagerDuty Services API. This is necessary since Pagerduty doesn’t support cross-domain JavaScript calls yet (Feb 2013)
  • 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.

Comments are closed.