How to Build a Hacker News Clone with Syncano (Part 2: Schedules)

Todd WackerTodd Wacker

Get started with Syncano quickly by building a Hacker News clone.

This is a continuation of Part 1, which can be found here.

Step 5: Creating a Schedule Socket

Now that we have our data, let's add a new feature to this app. What if we wanted to "review" and delete news items that didn't have any upvotes each day?

To do this, we're going to create a Schedule Socket. This Socket will run a specific Script at the interval or schedule you set. Scripts are snippets of code that can be run to perform custom functions in Syncano. These custom functions can be anything your creative brain can think of and are a great way to interface with third-party APIs.

Scripts can currently be written in Node.js, Go, PHP, Python, Ruby, and Swift. If you know any third-party API libraries that utilize one of these runtimes, you can include them directly in your Script just like you would normally. Most common APIs are supported, but if the one you want to use isn't, simply make a request on GitHub, or via the blue "?" at the bottom of the Dashboard, to add it.

In our case, we want to write a Schedule Socket that will check our news items and delete those with no upvotes on a daily basis -- let's say 12am UTC.

First, let's go back to our Sockets page so we can add a Schedule Socket.


Then click ADD in the Schedule Socket box.


The next screen you'll see is where you fill out the details for the Schedule. Create a Script called clean_up by typing that name in the "Script" input field. Then set the CronTab, which tells the Schedule Socket how often it should run, to "Run once a day at midnight" or 0 0 * * *. Lastly, set the time zone to UTC (you can also pick your time zone if you'd like).

Make sure it all looks like this:


Next it will open the Script creation screen, which you will fill out as follows:


Congrats! You've done it. Now we're going to open this Script so we can add the code (otherwise your schedule will run and nothing will happen!).

Step 6: Editing the Script

Assuming you haven't left the Sockets page yet, click on the link in the "Script" column of your new Schedule Socket labeled clean_up to open that Script.


This is a just a shortcut -- you could instead click "Scripts" under "Snippets" using the left sidebar, and then click on clean_up. Your screen should now look something like this:


On this screen, you can enter code into the code editor. For our Script, we'll enter the following code.

var Syncano = require('syncano'); // import Syncano JS Library  
var connection = Syncano({ // set up API connection  
    apiKey: CONFIG.apiKey,
    defaults: {instanceName: META.instance} // the Instance this Script is in
var className = CONFIG.className; // setting the class name

// List all of the objects in your Class
connection.DataObject.please().list({className: className})  
  .then(function(items) {
    items.forEach(function(item) {
      if (item.upvotes === 0) { // if no upvotes, delete object
        var itemToDelete = {
          className: className
        // then delete the item through the API
          console.log("Item successfully deleted"); 

This Script calls Syncano using the JavaScript library. It pulls the list of news items and checks for the ones with no upvotes. It then deletes those news items.

Before we can run this Script, we just need to add values to a few config variables. This can be done on the right hand side of the code editor where it says "Config":


Any config variable you add can be accessed in your code. For example, in Node.js you can use the CONFIG.keyName syntax as seen in the code above. Read more about CONFIG here.

We'll need config variables for apiKey and className to get this Script to run properly.

The apiKey config will be the same API Key we used earlier, and className should be set to news_item.

Once we have everything configured properly, let's test the Script. Go back to your news_item Class and create a new Data Object. Give it the title should delete and make sure it has 0 upvotes.

Return back to the Hacker News tab to make sure your should delete news item is there.

Now, go to the Script we just wrote and configured and click "Run". The Script should run without any errors. Go back to the Hacker News app and the should delete news item should no longer be there!

Since we set up the Schedule Socket, this Script will run every day at 12am UTC and delete any news items that have zero upvotes.

What's Next?

There are an infinite amount of things you could do next with Syncano. Here are a couple of suggestions!

Even More Sockets!

Sockets, as we mentioned in this guide, are your building blocks on Syncano. There are many things you can do with them, like schedule tasks, trigger Scripts based on data changes, deliver real-time data updates and Push Notifications to your users, and more! Read more about Sockets here.

Example Apps

Here you will find Example Apps that you can build on Syncano. They will help you understand how to use more features of the platform, as well as show you how to use our various libraries and SDKs.

Build powerful apps in half the time

Use our serverless platform to set up your backend in minutes.

Learn more
Todd Wacker

Todd Wacker

Dev Evangelist, Developer