I started the Easter Break with an idea for a website: a real-time Twitter stream of people trying (and failing) to spell the word ‘definitely’.
Let’s cut to the chase, here’s what I ended up building: defiantly-not.com
Sounds like a pretty dumb project idea, I know, but I’ve been looking for an excuse to work with the Twitter Streaming API and this seemed like a good candidate.
Here’s what I wanted the app to do:
• Find Tweets (in real-time) that mentioned the word “defiantly”
• Display those Tweets on a webpage
• Include a quip alongside the Tweet (e.g. “@john didn’t listen in school!”)
After a quick search I found a project written using Node.js & Socket.io which supported connecting to the Twitter Streaming API and display Tweets on a webpage. Awesome, I’ve also been meaning to play with Node.js and Socket.io, so this would be the perfect groundwork for my new app. The project is called Tweetstream and it lives here: https://github.com/samstarling/tweetstream
Using this code, I built an initial version of my app in an hour or so. Using a free Heroku account to host the website was a doddle. I’ve just started using Heroku, and it’s freakin’ awesome, very quick and easy to use, and free for low-bandwidth projects.
Tweetstream was great for getting my project off the ground, but did have an issue handling multiple HTTP connections. This was due to the fact that Tweetstream starts a Socket.io server instance when handling GET requests to the root of the site (see here). To fix this issue, I separated the application into two projects:
• Defiantly-Not, the front-end of the site. Handles the Socket.io client connection and rendering tweets to the browser.
• Defiantly-Not-Daemon, the back-end server. Handles connecting to the Twitter Streaming API and broadcasting new Tweets to socket.io clients.
Now, whenever a user connects to defiantly-not.com, a socket.io client connection is made to a single instance of a socket.io server (a defiantly-not-daemon hosted on a separate Heroku dyno). This allows for all connected users to receive the same Tweets at the same time.