Performing a full bootstrap + rendering a view is very expensive and can negatively affect the performance of the site, if done at each refresh.

Instead, it is suggested to let the module accept an additional, arbitrary URL that can be hit at each refresh interval to determine whether there's something to refresh or not. Because this is a hand-made script, it will be much more efficient than a full-scale view rendering.

Comments

infojunkie’s picture

Status: Active » Fixed

Implemented in latest dev. Here's how it works:

The additional theme parameter ping_base_path is used to inform Views Auto-Refresh that this URL should be hit before the secondary view. The script at this ping URL should accept a GET parameter called timestamp and return a JSON response of the following form:

array(
 'pong' => $count,
);

where $count is 0 if there are no new items since timestamp, > 0 otherwise.

For example, here's a complete ping script:

require('../../../default/settings.php');

global $db_url;
$p = parse_url($db_url);
$mysql = mysql_pconnect($p['host'], $p['user'], @$p['pass']);
if (!$mysql) die('Could not connect to database server.');
mysql_set_charset('utf8', $mysql);
if (!mysql_select_db(str_replace('/', '', $p['path']), $mysql)) die('Could not select database.');

$timestamp = mysql_real_escape_string($_REQUEST['timestamp']);
$type = mysql_real_escape_string($_REQUEST['type']);
$sql = "SELECT COUNT(nid) FROM node WHERE type='" . $type .  "' AND created > " . $timestamp;
if (!empty($_REQUEST['uid'])) {
  $uid = mysql_real_escape_string($_REQUEST['uid']);
  $sql .= " AND uid = " . $uid;
}
$count = mysql_result(mysql_query($sql, $mysql), 0);

print json_encode(array(
  'pong' => $count,
));

mysql_close($mysql);

This script illustrates parsing the Drupal MySQL connection string, getting a number of parameters including timestamp, and returning the expected JSON response.

infojunkie’s picture

The Views Auto-Refresh theme function should be called with an additional setting ping_base_path as such:

print theme('views_autorefresh', 5000, views_get_current_view(), array(
  'view_base_path' => 'liveblog/autorefresh', // the path of the update view
  'view_display_id' => 'page_2', // the display of the update view
  'view_name' => 'liveblog', // the name of the update view
  'sourceSelector' => '.view-content', // selector for items container in update view
  'targetSelector' => '.view-content', // selector for items container in on-page view
  'firstClass' => 'first', // class name for first element (optional),
  'lastClass' => 'last', // class name for last element (optional),
  'oddClass' => 'odd', // class name for odd elements (optional)
  'evenClass' => 'even', // class name for even elements (optional)
  'ping_base_path' => drupal_get_path('module', 'liveblog') . '/liveblog.php?type=report, // ping URL (optional)
));
l00ph0le’s picture

Any idea if this will work in D7? Looks great btw, exactly what I am looking for.

infojunkie’s picture

I'm currently focusing on D6, no plans to move to D7 unless someone else wants to undertake the port.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Reworded.