Skip to content

Instantly share code, notes, and snippets.

@WyriHaximus
Created November 21, 2013 11:14
Show Gist options
  • Save WyriHaximus/7579897 to your computer and use it in GitHub Desktop.
Save WyriHaximus/7579897 to your computer and use it in GitHub Desktop.
This bit of react/http-client listens on the twitter streaming API.
$buffer = '';
$request = $this->client->request($method, $url, $headers);
$request->on('response', function($response) use (&$buffer) {
$response->on('data', function($data) use (&$buffer) {
$buffer .= $data;
if (strpos($buffer, PHP_EOL) !== false) {
$tweets = explode(PHP_EOL, $buffer);
$buffer = array_pop($tweets);
foreach ($tweets as $tweet) {
if (strlen(trim($tweet)) > 0) {
$tweetObject = json_decode($tweet);
$this->saveTweet($tweetObject);
}
}
}
});
});
$request->end();
@johnhunt
Copy link

I look forward to giving this a try with our campfire chatroom.. hopefully using SSL isn't harder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment