Skip to content

Instantly share code, notes, and snippets.

@cryks
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cryks/54cbae21cea022daa035 to your computer and use it in GitHub Desktop.
Save cryks/54cbae21cea022daa035 to your computer and use it in GitHub Desktop.
<?php
if (empty($_REQUEST['since_id'])) {
$since_id = 0;
} else {
$since_id = (int)$_REQUEST['since_id'];
}
$db = new PDO("pgsql:...", 'user', 'pass', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
));
function retrieve($since_id = 0, $limit = 10) {
global $db;
$stmt = $db->prepare("SELECT * FROM timeline WHERE id > :since_id ORDER BY id DESC LIMIT :limit");
$stmt->bindValue(':since_id', $since_id);
$stmt->bindValue(':limit', $limit);
$stmt->execute();
return $stmt->fetchAll();
}
function output($val = array()) {
header('Content-type: application/json');
echo json_encode($val);
exit;
}
set_time_limit(45);
$timeline = retrieve($since_id);
if (empty($timeline)) {
$db->exec('LISTEN timeline_update');
$result = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 30000);
if ($result === false) {
output();
}
$timeline = retrieve($since_id);
}
output($timeline);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment