Skip to main content
All CollectionsAPI
Data synchronization for local storage
Data synchronization for local storage
Disqus avatar
Written by Disqus
Updated over a week ago

Since our WordPress plugin automatically syncs comments, we're going to use it as the example on how to sync your comments locally, both for backup and SEO. This is a three-step process:

  1. Set up a datastore so you can store comments locally.

  2. Synchronize comments from Disqus to your datastore.

  3. Fetch and render comments on pages.

Set up a datastore

First things first, you're going to need a datastore for comments. Here are the columns we recommend:

  • comment_id (char 32)

  • thread_id (char 32)

  • thread_link (char 200)

  • forum_id (char 32)

  • parent_comment_id (char 32)

  • body (text)

  • author_name (char 200)

  • author_email (char 200)

  • author_url (char 200)

  • date (datetime)

There are additional datapoints you may store as noted within the API documentation.

Synchronize comments

Now that you've set up your storage, you're going to need to build a synchronization task. The way we do this in the WordPress plugin is an incremental backup โ€” we, at most, hit the Disqus servers once every 10 minutes. When we query Disqus we only ask for "posts newer than the last post you sent me". In pseudo-code this would be something like:

// query our MAX datestamp
$since = "SELECT MAX(date) FROM posts";// fetch newer than posts from API
$posts = $DisqusAPI->forums->listPosts({
   'forum': 'my_forum_shortname',
   'order': 'asc',
   'since': (int)$since,
   'limit': 100
});// save posts locally
foreach ($posts as $post) {
    save($post);
}

Don't try to fetch all posts at once. Instead, perform multiple queries to the server and take advantage of the "since" query to ensure no posts remain to be synced. Remember that you must change the sort order to asc (oldest to newest) when using the since parameter to get all comments from that date until now.

If you did want to achieve a full backup (and you don't exceed the API request quota) this could be achieved with something like:

// continue fetching posts until we get less than 100 results back
do {
    $results = fetch_posts($limit=100);
} while (count($results) == 100);

Fetch and render comments

Finally, now that you've stored your posts locally, you're going to likely want to render them server-side so that search engines may pick up their content:

 
    [... output your server-side comments here ...]
 

    [... Disqus universal embed code ...]
Did this answer your question?