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!

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.