Pusher is the perfect platform for turning a static, REST-based API like Reddit’s into a smooth, realtime API utilising the power of WebSockets. The best bit? You don’t need to change anything with an existing REST API to do this!

We love Reddit at Pusher. You don’t have to take our word for it though, with over 731 million unique visitors last year, and over 40 million story submissions, it’s a pretty big deal however you look at it. It’s a weird and wonderful place, whether you want to keep up to speed with programming and JavaScript, or simply just want to see beautiful photos of planet Earth, Reddit has it all (and then some).

While Reddit is great, it’s a static experience in which you have to refresh the website to see new stories — you can’t sit back and let them come to you as they happen.

The solution? Building a realtime Reddit API that allows you to subscribe to a subreddit (content on a particular topic, like JavaScript) and receive every single public story posted within it. This is what we’ve built, open and free for everyone.

Use-cases for the Pusher Realtime Reddit API

While a realtime Reddit API sounds amazing, what can you actually make with one and why is it useful?

Streaming feed of new stories as they’re posted

The simplest and most obvious thing to do is to provide a realtime feed of stories in some sort of Web app, allowing you to sit back and consume Reddit as it happens. Sort of like Twitter for Reddit.

Email alerts when keywords appear in stories

For a little more productivity, you could automatically monitor the realtime feed of story submissions and monitor it for particular keywords (eg. your business or product). By then using something like SendGrid to send an email alert you can have an immediate awareness and response on Reddit as events occur of interest you.

Posting relevant stories to a chatroom (like Slack and Hipchat)

Like the email alert example, you could also use the realtime feed to monitor keywords and have stories automatically published to wherever you and your team would prefer the information to be displayed (like a Hipchat or Slack chatroom).

And more…

There are plenty of other uses for an API like this that haven’t even been dreamed up yet. With such a wealth of data at your fingertips it’s nearly impossible not to find some way to use it productively.

API request and response format

The Pusher Realtime API follows a basic structure. Here is an overview:

  • The Pusher application key for the realtime Reddit API is 50ed18dd967b455393ed (you’ll need this to connect to it)
  • Each subreddit publishes to a Pusher channel using the name of the subreddit in lowercase and without the “/r/” prefix (eg. “askreddit”)
  • New stories are published using the new-listing event (how to subscribe)
  • Stories are formatted using the standard Reddit API JSON structure

That’s it. We’ve tried to keep things as close to the Reddit REST API as possible.

Using the API

The Pusher Realtime Reddit API has been built with simplicity in mind. All you need to do is subscribe to any subreddit using one of Pusher’s free platform libraries and decide what you want to do with each story.

Here’s an example that subscribes to the “/r/AskReddit” subreddit using JavaScript and outputs each new story to the browser console:

<!-- Include the Pusher JavaScript library -->
<script src="http://js.pusher.com/2.2/pusher.min.js"></script>

<script>
  // Open a Pusher connection to the Realtime Reddit API
  var pusher = new Pusher("50ed18dd967b455393ed");

  // Subscribe to the /r/AskReddit subreddit (lowercase)
  var subredditChannel = pusher.subscribe("askreddit");

  // Listen for new stories
  subredditChannel.bind("new-listing", function(listing) {
    // Output listing to the browser console
    console.log(listing);
  });
</script>

You can see a working, interactive code example of this on JSBin. It’s not a huge step to take things from here and start visualising new stories in some way.

Try the demo and build your own realtime Reddit app

We’ve built a simple demo that lets you subscribe to realtime updates from any public subreddit (viewer discretion is strongly advised).

Pusher Reddit Realtime API Demo

Give it a go and see how it inspires you to create your own application using the Pusher Realtime Reddit API. If you’re looking to dive into the good stuff, you can find the Github repo here.

Thinking of making something with this free, no-limits API? Let us know when you have, we’d love to see it!

  • Wesley

    *Love* the idea, but the demo is not working for me. It’s been ‘listening’ (all 4) for well over a minute now.

    • Wes

      I’m an idiot, and I should be ashamed. It’s still early, apologies :<.

      • http://rawkes.com/ Robin Hawkes

        No problem, it happens to all of us. Let me know if you have any other questions.

  • WhyDoesThisNotWorkWell

    I’ve been trying to use the API (and tried the demo) with the subreddits:
    leagueoflegends
    starcraft

    I’ve given the demo app 10+ minutes and I’ve tried to subscribe to these and get the listings on my own as well with no success. Am I confused about which subreddits would be available (these are “public” as far as I know, and your demo autofills for them) or is this just poorly hacked together by a developer advocate?

    • http://rawkes.com/ Robin Hawkes

      Were submissions posted to those subreddits while you were listening? The API and demo currently doesn’t backdate submissions. If there are no listings appearing for a subreddit then that subreddit is likely not available via the public Reddit API (which is what we hook into). We’ve been testing this app for a week and, although not perfect, it’s stable and has been working great for the subreddits we’ve tested on.

      Let me know if you have any other questions.

  • ncerminara

    Hopefully you guys are still around. I’m using Pusher for a work related project and am loving it. You guys are awesome. I was thinking about writing a tutorial on it for my blog at scotch.io.

    Anyways, with this, is it only realtime, or is there a way to pull from the API say the last 500 posts before the realtime stuff kicks in with Pusher? Or, is there a “History” API. So I could get realtime results from say 2 years ago? I’ve seen stuff like that with third-parties who provide access to the Twitter Firehose. Hoping there’s something like this with Pusher.

    Generally just looking for some additional docs besides the reddit example. Thanks!

    • http://rawkes.com/ Robin Hawkes

      We’re still around! That’s awesome that you’re using us for a project, I’m glad you’re happy with us. Let me know if you do write a tutorial, I’d love to see it.

      As things stand, you can only consume Reddit posts in realtime. It’s possible in the future to add a historic log though it’s not something we’ve looked at yet as we didn’t want to overcomplicate the first version of the API. Everything the current API offers is documented on the GitHub repo.

      The other public API we’ve released is for Product Hunt, you can find out about it here: http://pusher.com/tutorials/product-hunt

  • Nico

    Hi there, nice API, but where’s the methods documentation? I’m kinda lost, can I use it to keep track of a single thread? See how people vote it and stuff? Thanks ;)

About Robin Hawkes

Robin thrives on solving problems through code. He's a Digital Tinkerer, Head of Developer Relations at Pusher, former Technical Evangelist at Mozilla and book author. In his spare time he experiments with new technology and data visualisation, often at the same time.