-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreddit-async.php
37 lines (30 loc) · 941 Bytes
/
reddit-async.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
use ApiClients\Client\Pusher\AsyncClient;
use ApiClients\Client\Pusher\Event;
use React\EventLoop\Factory;
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
$loop = Factory::create();
/**
* The App ID isn't a secret and comes from a Pusher blog post:
* @link https://blog.pusher.com/pusher-realtime-reddit-api/
*/
$client = AsyncClient::create($loop, require 'reddit.key.php');
$subReddits = \Rx\Observable::fromArray($argv)
->skip(1)
->flatMap(function ($subReddit) use ($client) {
return $client->channel($subReddit);
});
$subReddits->subscribe(
function (Event $event) {
echo 'Channel: ', $event->getChannel(), PHP_EOL;
echo 'Event: ', $event->getEvent(), PHP_EOL;
echo 'Data: ', json_encode($event->getData()), PHP_EOL;
},
function ($e) {
echo (string)$e;
},
function () {
echo 'Done!', PHP_EOL;
}
);
$loop->run();