Beginner-friendly access to the Reddit API using Node-RED

Node-RED

Node-RED is browser-based visual programming environment that is built on Node.js. The idea is to create flows by wiring nodes together. Nodes can only send/receive JavaScript objecst and when in doubt, you can always write JavaScript code in a function node. If you're learning to program, I'd recommend using the function node as much as you can to get familiar with JS syntax.

Node-Reddit

My team recently published an NPM module that provides easy access to the Reddit API in Node-RED. With it, you can get your feet wet with APIs without having a stray curly bracket ruin your day. Sorry for the self-promotion, but I really think this could be an easy, fun project for beginners!

Getting started

You'll need a Node-RED application. It's very easy to setup on Windows or Linux if you follow these instructions. You can also create a free IBM account and deploy a Node-RED app on their cloud. If you're running locally, you can view the editor at http://localhost:1880.

Once your app is running, click settings -> manage pallette and search for node-reddit to install our module.

Create a Reddit app

I outlined the instructions for this here. Basically you need to log into your Reddit account, visit this website, and create an application (choose script if you're not sure). This will give you your Client ID (upper left corner) and Client Secret. Save these, because you'll need them for later.

Setup your credentials

Drag any of our Node-Reddit nodes into the editor and open it. You'll see this screen at the top. Click the edit button and you'll see this screen. Enter the Client ID, Client Secret, your username and your password. The user agent is a short description of what your app does.

First flow

It's best to create your own flow before you import one of ours. Start by dragging an inject node into the editor. Drag a debug node into the editor and have it display the full msg instead of just msg.payload. Wire them together and click inject. You should see something like this where the debug window is showing a JavaScript object with a Unix timestamp in msg.payload.

Now insert a Node-Reddit get node in the middle of inject and debug. Configure it by selecting your credentials, entering a subreddit name, and setting limit to 10. You should see this. Now start the flow by triggering the inject node. Your window should look something like this. The get node returns 10 JavaScript objects, each of which contains a Reddit listing in msg.payload. Each listing contains various details about the submission (votes, author, subreddit, etc.).

Dynamically populate fields

Let's do the same thing, but without hardcoding a subreddit in the subreddit field. We can dynamically populate the field using mustache syntax relative to the incoming msg object. And it's actually very easy to do.

Open the inject node and make a msg.payload a string representing a subreddit. Now open your get node and enter {{payload}} in the subreddit field like this. Trigger the flow and you should get the same result.

This is how you can build meaningful flows using our nodes. Use the stream, get, and search nodes to get Reddit content, and then access the listings by entering {{payload.id}} in the id field of the following node.

Import our flows

We made a few example flows to help you get started. Let's import the first one. First, copy the contents of the JSON file here. Go back to your Node-RED app and go to settings -> import -> clipboard. Now paste the JSON and click import.

The flow will appear. Drop it anywhere you'd like. You'll have to configure the get node by selecting your credentials. Now, go to /subreddit. If you're running the app locally, this should be http://localhost:1880/subreddit. You should see an empty table. Enter a subreddit in the text field to make an AJAX request. The table will populate with the top 10 hot posts from the subreddit you entered.

We also made flows to help you create a bot, overwrite and delete your comments (for privacy concerns), and do some basic sentiment analysis on a subreddit's comments.

Feel free to edit these flows, tweak them, break them, and make them your own.

Credit

We used the Snoowrap and Snoostorm packages a lot in our code, so big thanks to them. The Node-RED developers have also been very helpful whenever we had any questions.

Hope you enjoy and thanks for reading!

I am not sure what this topic is about, is it about introducing the reddit node ? Then it should perhaps be moved to the nodes category.

I see your node has a series of example flows. If you change the directory name from
flows to examples and update the node, these example flows will be included and available for users directly from the node red editor screen.
See https://nodered.org/docs/creating-nodes/examples

I think they just cut and pasted from a blog to save re-editing it for here

I do wonder how big the differences are between the 3 published reddit nodes. It also explains why this is named node-red-contrib-node-reddit and not easier node-red-contrib-reddit: because that name was already in use

An overview of reddit nodes:



Well at least the included nodes differ a lot. This one seems the most comprehensive based an a quick glimpse.

Welcome to the forum and it looks like an interesting node.

However, it really is helpful if you don't just copy/paste from somewhere else without giving some context at least with an introduction. Personally, I tend to write some intro text, then add a horizonal line separator and then post the copy.

Otherwise people tend to get very suspicious that you might be trying to spam the forum - something that unfortunately happens from time-to-time.

Also helpful if you let us know if you tried to reach out to the authors of the other similar nodes. It quickly becomes confusing to have many similar nodes. Did you make any effort to reach the other authors and collaborate with them?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.