I am constantly learning as I write and refactor my code. It’s what makes code I wrote a week ago look awkward and clunky next to my daily output.
Last August I wrote a python script to manage push notifications for Prose, my App.net client. Today, I still barely have a grasp of python, yet when the script began crashing I looked at it again and saw flaws I would never have seen back in August.
I knew I needed to make something more future proof, and quickly. The Python script was terrible for a few reasons and my new solution needed to solve these problems:
- The Python script was like a good stew. It was a single massive pot full of model objects, networking functions, and confusingly named variables. It was awful.
- The script used synchronous functions exclusively. I don’t even know how this was possible.
- At the time, I had a significant amount of help writing the script so I didn’t fully understand the API endpoints or native python functions I was calling. I didn’t understand why things were crashing.
- There isn’t a user friendly or transparent way of keeping a python script running indefinitely. My solution involved the crontab and was hacky at best.
- When my script inevitably crashed, it didn’t cry out in pain before giving up the ghost. My only feedback was mentions that wouldn’t make it to me.
I had a vague idea that Node was great for server side applications but after I heard Casey Liss talk about his experience with it I became curious.
When I started looking past my own hipster-esque preconceptions of Node I found that many of my problems are solved by the language or community surrounding it.
The New Script
Delivering push notifications to users involves two steps: Fetching and monitoring a long lived HTTP connection and transmitting the received data to Parse for delivery to devices.
I decided to separate these two tasks into two different modules; with my first I learned the ins and outs of Node. It’s simple but it can:
- Handle authentication.
- Create, delete, and fetch both filter and app stream objects.
- Monitor a fetched app stream with a simple callback.
Eventually I published this module to npm under the name
stadn. You can read more about it on Github.
My second module transforms JSON response envelopes (as described in the docs) into native objects, assembles the proper APNS payload, and then delivers the payload to Parse. This whole process is quite specific to my use case so I opted not to publish the module.
When these modules were tested and complete I combined them into a very manageable script that is a mere 54 lines long.
Concise code is great but what about performance? Well, Node’s
http modules both employ network request functions that take callbacks making it near impossible to block the main process. As an added bonus, if the script ever fails and is run again, it receives the unconsumed notifications from the stream and can handle the excess response envelopes as fast as they come in. I’ve seen my script simultaneously deal with 12+ response envelopes easily.
At this point I’ve become familiar with Node and the necessary App.net endpoints and have modular code executing asynchronously. My first three problems are solved.
Keeping my script up and running was the easiest problem to solve. One Google search and two lines of code was all it took. Check out the
forever module, it’s really cool and provides great extensibility and a super simple CLI.
I’m not accustomed to working with headless environments, error reporting on a server has always baffled me. To solve my final problem my new script sends me a notification every time it’s started. Encapsulating code into separate modules allowed me to add this functionality with just two lines of code.
After the first day of playing with Node I could see its potential. The language is simple, easy to read, surrounded by a large community, and the built in tools make it extensible.
I haven’t had a single crash since the script was first put into a production environment and I’m ecstatic about those results. It doesn’t matter if the Node community is a bunch of hipsters like I originally assumed because Node is useful. I now have another tool in my tool belt and the confidence to use it.
If you want to play around with App.net’s App Streams, check out my first module on Github. It’s my first Node project, please don’t hesitate to point out errors or optimisations. If you have any questions, I’m @shawnthroop on all the things.
This post was originally posted in 2012. I chose to touch it up and repost it here because it still rings surprisingly true two years later.
I recently listened to an episode of Enough, a podcast with Patrick Rhone and Myke Hurley, in which they talk about Myke’s “undiagnosed ADD” habits while writing. They discussed a lot more than that, as usual, but when Myke was describing himself I found him describing me as well. It was a bit freaky.
Myke mentioned that he often listens to music while writing because it helps to keep him on task. I do the same so I thought I should share my favourite tracks as Myke did his:
Tirol Concerto for Piano & Orchestra - II
This track is 16 minutes long and it puts me in a little bubble of soothing bliss for its entirety. It was composed by Phillip Glass so naturally the song is layered with emotion, strings, and the intense piano scales that he is famous for. Despite the sixteen minute long running time, I have played it forty three times. Download: iTunes, Amazon
The Social Network
The track Magnetic sticks out for me because it is full of electric guitar and complicated beats but yet remains subdued. Like all tracks on this album, it has a distinct feel and slowly builds to a great crescendo. Download: iTunes, Amazon
The Girl With the Dragon Tattoo
This soundtrack is very similar to The Social Network soundtrack because they were both composed by Trent Reznor and Atticus Ross. They both incorporate a lot of non instrumental content and, again, lots of complicated but subtle rhythms with slow builds. Download: iTunes, Amazon
This soundtrack is brilliant because it integrates grand orchestral pieces with Daft Punk’s unique electronic rhythms and bass. If you’re a geek you will probably already know how amazing this album is but it’s worth noting that iTunes and Amazon each have their own exclusive tracks. Download: iTunes, Amazon
This one is different because it’s a curated collection by Hector Simpson that’s wrapped up as a site. Each Tuesday Hector posts links to an album and lets you stream his favourite song on the site. I like to think of them as a bunch of piano and electric guitar solos that are really chill. From what I have heard (and bought) they don’t have lyrics. Highly recommended. Site: Postrock Tuesday, Favourite track
Pirates of the Caribbean - The Curse of the Black Pearl
This soundtrack is a lot of fun. If I’m already slightly focused this album keep me in that groove. I’ve kept this one on the preverbal shelf for a while, after another listen its back on my regular rotation, and not just for while writing. Download: Amazon
The Bourne Identity
This is one of the first albums I owned. It’s womderful, has lots of variation, and incorporates a lot of non-instrumental elements that I feel were a bit ahead of the curve. Nowadays every other action thriller uses these types of non instrumental scores. Download: iTunes, Amazon
The Bourne Supremacy
This soundtrack is just as good, if not better, than it’s predecessor The Bourne Identity. I enjoy how each track feels singular but when taken as a whole they are even stronger. I’m a sucker for violins and is album delivers. Download: iTunes, Amazon
After listening to all of these again while writing this post I honestly can’t pick a favourite. I could try ranking them in some order but that would be agonizing and stupid. Like pairs of shoes are designed for an occasion, each of these selections are perfect for a specific occasion.
If you’re trying to piece together why these tracks and albums fit my writing environment you should really listen to the episode of Enough. Adding to Myke’s explanation, they fill the silence and cover up the small noises that annoy my brain and dislodge it from the task of writing. Some music, like music with lyrics, doesn’t help. Unfamiliar and new albums are even better at derailing my attention. If a track that does help me concentrate is playing I feel like the part of my mind that would wander is already occupied in the back of my brain and the part of my brain for writing is in the foreground, ready and focused.
I think it is a lot like using a TV program in the next room to keep a child happy while I work in another.
In the past I avoided putting my name on things. However, after I released an app in August I realised I needed a place where people can find out about me and what I make.
After my Linode accidentally got wiped this summer, I was given the perfect opportunity to start over. So, I cloned a copy of Octopress and I now have my little corner on the internet.