depicus

changing the digital world one bit at a time...

Blants (noun) the rants of a blogger

Wake on Tweet – because you never asked for it…

December 30, 2014 by The Man
Wake on Tweet

Wake on Tweet

tl:dr Just send a tweet with the hashtag #wakeonlan and a properly formatted mac address like AA:BB:CC:DD:EE:FF, an ip address or FQDN and a port number and that is it. Easy.

It all started a few weeks ago when I was looking at Slack to use at a clients site so we could integrate more. If you haven’t tried Slack I would highly recommend taking a look – it’s WhatsApp for groups with a steroid injection of crazy fun stuff for developers. One of those fun things are hooks where you can define actions when things happen. So I have Slack hook alert to a group called #twitter whenever a tweet mentions @depicus it would call a webpage on a server. Sadly the info Slack passes wasn’t enough for me as it doesn’t include the message body of the tweet. So sadly my brave idea of Wake on Lan via Twitter died….

Now I’ve often read those articles about Hackathons and never fully understood how they manage such things, indeed I was reading about creating a private Heroku web server when I thought it was about time to have my own sofa based hackathon.

So first thing was to think about how to get the info from Twitter. At first I was looking at polling Twitter every second using the REST API but looking at the rate limits this isn’t really meant for constant monitoring. Enter Twitters Streaming API which is perfect for the job. Streaming is, as the name suggests, HTTP streaming of data in a permanently open connection (in an ideal world).

Knowing this was going to be an application rather than a web site I had a quick search of the interweb to see if there was any code for Twitter Streaming and as ever the interweb delivered big time. Twitter4J is a java library which exposes all the Twitter API calls you would need. Next let’s think about hosting…

If you haven’t heard of Heroku before it’s a cloud based app provider, a sort of halfway between a hosted site and a hosted virtual machine. I really like it mainly because you can fire up a app for anyone of the following techs Ruby, Node.js, Python, Java, and PHP and if you run a single node (or dyno as they call them) it is free. It also doesn’t need to be a web app, you can fire up worker processes and this is exactly what I needed.

So once I’d signed up for a free account (or pay if you want to but they are a bit expensive) it was a simple

git clone

to get a template ready to start. You can get the full instructions from here and if you follow the instructions it’s quite easy to get up and running. It was also my first time using Maven, again it is something I had seen but never really looked into because often coding has deadlines and you don’t always get time to learn something new. Boy I wish I’d taken the 5 minutes it took to learn, makes adding dependancies so much easier.

So for a worker process you need to make a few amendments. The Procfile needs to start with

worker

instead of

web

but otherwise there is nothing too hard and different from the web app. One gotcha which took most of my time was the classpath. When I was testing on the local machine it was fine using the deploy script they recommend but was failing one I uploaded.The answer was to copy the classpath setting from the test script to the Procfile.

So after starting at 9am and a few breaks later it was 10pm and I have a fully functioning java worker process in the cloud listening for the #wakeonlan hash tag and then processing the tweet to see if it has the information we need and then sending a wol packet. Add in a bit of logging from Loggly.com so I can check what is happening and I think that was a good days geeking out 🙂

If you have any questions feel free to ask.

loggly