Reddit provides a wonderful system for voting stories up and down, discussing the stories along with new ites from each subreddit a user has subscribed to appearing in their news feed. The subreddit system is ideal for building a community about a very specific topic. Because of this, I’ve decided to provide comments, discussion and discovery of articles for my blog via Reddit’s subreddit model. And here is how I did it.
Once logged in, it’s extremely easy and quick to get a subreddit set up on Reddit. While it’s all really straightforward, there is one option that you would want to make sure is set. That is setting the Type to ‘restricted: anyone can view, but only some are approved to submit link’. I chose to customize the CSS of the subreddit to match the look-and-feel of the blog. There may be some guides out there on how to do this, I simply viewed th stylesheets affecting each element I wanted to change and added an entry to reflect what I wanted. The end result was about an hour or two of playing around and testing things out. The subreddit is located at http://www.reddit.com/r/ismarc.
The initial integration with Reddit is as simple as adding a bit of Javascript to the page and properly setting up the environment. The initial integration is simply a button provided by Reddit. There’s a large number of choices for the format and information displayed. Details about implementing it are available at http://www.reddit.com/buttons/. I chose to go with an interactive button as I wanted readers to be able to up and downvote the particular article without having to leave the site. The button is placed wherever you put the script tag for the button. The plus side is if you set everything up properly, you can view your post and then submit it from there immediately. The relevant portion of my post template ends up looking like:
With the initial integration completed, readers can see how the article has been voted on by others. The tiny Reddit alien head on the left of the up and down arrows links to the discussion about the post, but we want even more pervasive integration.
After a quick search, I was unable to find an existing solution for using Reddit as the discussion forum for another site. A quick search later and I found the Reddit API. It was designed to be consumed by a client application, but there is nothing preventing the client from being run in Javascript. The work is made much simpler by the API accepting a JSONP query parameter. The integration isn’t as complete as I would like, but it is a good starting point (I will cover what I would like to do later on).
There are two main endpoints that need to be used to get and display the comments for a particular post:
Now that we have a way to retrieve the data about the post, it’s just a matter of formatting and rendering the content how we want to. The quickly and horribly written Javascript I use is available at https://github.com/ismarc/ismarc.github.com/blob/master/javascripts/reddit_comments.js. It’s available under the MIT license and I will likely be cleaning it up and slowly building out to cover my dreams of the future covered in the next section. It is missing a lot of error handling and has hardcoded values in some places, but should at a minimum be a jumping off point for a similar implementation.
If you visit the comments page of any submission on Reddit, that is the full functionality I would like to provide access to. However, that requires login integration as well as cross-site POST requests for submission of data. Since it is a more involved process and has potential security implications, I’m not going to just throw something together. Instead, as I have time, I’ll work on implementing the system…unless someone else would care to put it together and let me know.