Some fun with Roomie Agent and Javascript

The first version of the API feature has been working out pretty great so far. I’m a web app developer (mostly front-end), so I whipped up a quick API tester, located here.

This gets my current activities and put them into a dropdown list. I can select an activity and click “go” to test.

It’s all pretty old-school for the moment (I’m looking at you, jQuery in 2015…), but now that I’ve got it working I’ll be starting in on a reactjs version with some real, custom UI.

One roadblock to this working properly is that the built-in server Roomie Agent uses doesn’t set proper headers for CORS. It’s entirely probable this is by design.

I wound up creating an HTTP Tunnel (I did mine via node, but you could probably do applescript or a bash command or whatever suits your fancy). The tunnel acts as a decorator, and runs on the same port I serve this JS upon.

It captures all requests going to /api, and finalizes a few things before making a backend request to Roomie Agent. This means I can use ajax calls on the front end (aka: no page refresh) without worrying about CORS restrictions in the browser.

Here’s a quick example of something that would work in node running express, but really you can do it in just about any backend language with CURL or whatever.

Note: this second gist is only a partial example. I have gulp running express for me and a bunch of other craziness. I also didn’t bother with an example that captures GET, which you’ll need to return the activities list. We’re focusing on POST in the example since it’s slightly more complicated. Also note we stringify the data before sending it to the agent on line 9.

To test, I created a quick activity that just runs through each input on my HDMI switch every time the “go” button is clicked. Works like a charm.

Next up I’ll be working on a small app for the Pebble watch using their JS framework to enable Roku and AppleTV control right off the watch. Having custom UIs and enabling them on devices not yet supported is really the holy grail of why I looked into Roomie Agent in the first place. Happy with how it’s working out so far.

Hope this helps someone else out.