Last active
August 29, 2015 14:12
-
-
Save cryks/54cbae21cea022daa035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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