Tuesday, January 8, 2013

Testing Webhooks from your Command Line with Node.js

Webhooks are a mechanism of defining your own HTTP callbacks for remote web services that are pushed data whenever an event occurs. Most importantly, it’s another tool for leveraging web services alongside an HTTP client.

The difficulty of webhooks is that you are no longer the client, and things which are easy to test locally (like accessing a REST API) suddenly become difficult from your command line. Since Webhooks must be public URLs, http://localhost:5000/ won’t cut it. Just experimenting with webhooks is nontrivial.

Very recently, the excellent and free Localtunnel v2 was released, allowing you to expose your local server at a public localtunnel.com subdomain of your choice, as long as it’s available. Smee (on Github) is a Node.js module that calls Localtunnel to create a persistent webhook that calls a local callback. A unique domain of the form smee045465383834645896.v2.localtunnel.com is generated and saved for your script, letting you maintain the same URL even after restarting.

Smee works entirely from code. Just define a new hook and listen for callbacks:

var smee = require('smee');
smee.persistentHook(function (err, hook) {
  console.log('Your webhook URL:', hook.url);
  hook.on('callback', function (payload) { /* webhook payload */ });
});

The following example uses the very minimal Venmo API, which exposes nothing but an ability to set up a webhook to trigger each time a payment is made or received. To install Smee, first install Localtunnel via easy_install localtunnel or pip install localtunnel. Next, npm install smee and run this script:

After a moment you should see something like the following in your console:

Your webhook URL: http://smee3484480480040896.v2.localtunnel.com ...

Copy that URL and go to your Venmo Developers Settings. Click the “Receipts API” option and set your callback URL:

Settings Callback

Click “Save Changes”. Next, click on “Your Applications” above “Receipts API”. You’ll see an entry called “Venmo Receipts API”. Copy the “Secret” key and paste it into your script, then stop and restart your script to load your secret key. (Note how the URL remains the same after the script is restarted.)

Make any test payment on Venmo and the receipt payload will appear in your console:

$ node venmo.js
Your webhook URL: http://smee3484480480040896.v2.localtunnel.com ...
[ { via: 'Venmo.com',
    publish_to_venmo: 1,
    pay_or_charge: 'pay',
    from_user: 
     { username: 'timcameronryan',
       twitter_username: 'timcameronryan',
       facebook_id: null},
    from_user_id: 56740,
    to_user_id: 0,
    to_user: 
     { username: ... } ]

Simple! Once you’re comfortable with how your code is handling a webhook push, it’s easy to move your logic to a permanent URL.

Localtunnel by Jeff Lindsay is free, though donations are accepted. If you’re interested in a permanent localhost tunneling solution, check out Pagekite that provides permanent URLs like http://tcr.pagekite.me and unlimited subdomains.

Tuesday, February 28, 2012
I’m enrolled in a class at Olin this semester called Real Products, Real Markets. Our first team project is to conceptualize, design, produce, and sell items cut from sheet metal, in just four weeks.



This is one of our scrapped prototype ideas: Social Media Charm Bracelets. Luckily these made it on to our test cut sheet, since how else would I be able to publicly declare my sweet Pinterest cred.

I’m enrolled in a class at Olin this semester called Real Products, Real Markets. Our first team project is to conceptualize, design, produce, and sell items cut from sheet metal, in just four weeks.

This is one of our scrapped prototype ideas: Social Media Charm Bracelets. Luckily these made it on to our test cut sheet, since how else would I be able to publicly declare my sweet Pinterest cred.

Tuesday, November 1, 2011
Tuesday, March 15, 2011

Controlling your mouse using an iPad and HTML5

A friend recently showed me the JazzMutant Lemur, a swank multitouch audio controller for synthesizers, and asked how he might duplicate its functionality with an iPad. Though it would definitely be possible in Objective-C, I told him the quickest way to get started would be to make an HTML5 app. To demonstrate, I made a full-screen mouse controller on the iPad, which connects to a Python server on a computer using WebSockets.

What you’ll need for iMousePad:

  • A Linux installation
  • An iPad (iOS 4.2+)
  • Both to be connected to the same network.

Read More

Monday, January 10, 2011 Thursday, October 28, 2010 Monday, June 14, 2010

Mug, a Java Compiler

CoffeeScript is my favorite new language at the moment. It’s a syntatically light preprocessor to JavaScript, influenced by Python and Ruby, that exposes the niceties of JS’s loose typing and lambdas without gaudy vars and (function () { ... })s littering your code. Once you start using its comprehensions and loop structures, it feels silly to go back to scripting Java-inspired syntax on the web.

Perhaps what I enjoy most about CoffeeScript is that it isn’t an entire language, just a syntax. Because of that, we can rather easily repurpose jashkenas’ work from compiling to JavaScript to any language—in this case, Java.

You can interactively play with a demo of Mug, a CoffeeScript-to-Java Compiler. It can take a subset of CoffeeScript code and compile it to valid (albeit gaudy) Java source. The resulting code, however verbose, is 1:1 with its input, accompanied by some generated class structs and scope objects. Objects in JavaScript become glorified HashMaps, and type conversions are large switch structures.

The results of such a hacky conversion are actually surprisingly fast on HotSpot. For the “Binary Trees” benchmark, the Mug compiled version runs about ~3–4x slower than V8 on my machine, but ~7x faster than Rhino, while still maintaining many of the benefits of a dynamic language (closures, prototypical inheritance, hashes, etc.) The question becomes you value the tradeoff of flexibility and speed.

As a CoffeeScript compiler, Mug is obviously lacking the features of a complete JavaScript implementation. But just as CoffeeScript was meant to improve the experience of programming on the web, Mug could conceivably develop its own semantics and be a light preprocessor for Java. While there are many alternativesto programming the JVM, I expect that a class of “little languages” like CoffeeScript that compile into more familiar languages will start cropping up. Rather than reinvent the programming experience, these simply make the existing programming experience programmers are forced into easier for day-to-day use.

Thursday, March 11, 2010

Passion Pit Pursuits

Olin has these nifty things called “Passionate Pursuits” where you get to take an (essentially) one-credit course per semester on a topic of your choosing. For example:

  • Ice Skating
  • Pottery
  • Clay Shooting
  • Glassblowing
  • Piano lessons
  • Slam poetry
  • Cooking for Singles

…you get the idea. You write up a proposal and get it approved though the Passionate Pursuits Board. You can even get funding, up to about $200, for lessons or equipment. It’s pretty ballin’.

Naturally Olin classes are workload enough and I’ve put it off first semester. But half-semester classes having just ended, I decided that starting a Passionate Pursuit midway through a semester (despite doubling the number of hours per week I’d need to work at it) would be better than nothing. I decided, ultimately, a good use of my time would be guitar lessons, since it’s been on my To-Do list for years.

Of course, the very week applications are due, I discover there’s no funding left. I’m left in the same boat as every other-last minute applicant. Whoops.

…until I realize, wait, I already have a guitar.

…and I already have lessons. It’s the Internet.

So paperwork be damned, I’m going to learn how to play guitar by summer. Just like Facebook, it needn’t be written down to still be official. (gg.)

Don’t you hate it when the only thing holding you back is thinking you need a reason?

Tuesday, March 2, 2010

LOAD “*”,8,1

Any professional web presence demands your wordly thoughts be serialized in words!

But what I really want, is to serialize my wordy thoughts into worlds.