-
Notifications
You must be signed in to change notification settings - Fork 31
/
demo-subscribe-to-all.php
77 lines (64 loc) · 2.45 KB
/
demo-subscribe-to-all.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* This file is part of `prooph/event-store-client`.
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
* (c) 2018-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventStoreClient;
use Amp\Loop;
use Amp\Promise;
use Amp\Success;
use Prooph\EventStore\Async\EventAppearedOnSubscription;
use Prooph\EventStore\EndPoint;
use Prooph\EventStore\EventStoreSubscription;
use Prooph\EventStore\ResolvedEvent;
use Prooph\EventStore\SubscriptionDropped;
use Prooph\EventStore\SubscriptionDropReason;
use Prooph\EventStore\UserCredentials;
use Prooph\EventStoreClient\Internal\VolatileEventStoreSubscription;
use Throwable;
require __DIR__ . '/../vendor/autoload.php';
Loop::run(function () {
$connection = EventStoreConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);
$connection->onConnected(function (): void {
echo 'connected' . PHP_EOL;
});
$connection->onClosed(function (): void {
echo 'connection closed' . PHP_EOL;
});
yield $connection->connectAsync();
$subscription = yield $connection->subscribeToAllAsync(
true,
new class() implements EventAppearedOnSubscription {
public function __invoke(
EventStoreSubscription $subscription,
ResolvedEvent $resolvedEvent): Promise
{
echo 'incoming event: ' . $resolvedEvent->originalEventNumber() . '@' . $resolvedEvent->originalStreamName() . PHP_EOL;
echo 'data: ' . $resolvedEvent->originalEvent()->data() . PHP_EOL;
return new Success();
}
},
new class() implements SubscriptionDropped {
public function __invoke(
EventStoreSubscription $subscription,
SubscriptionDropReason $reason,
?Throwable $exception = null
): void {
echo 'dropped with reason: ' . $reason->name() . PHP_EOL;
if ($exception) {
echo 'ex: ' . $exception->getMessage() . PHP_EOL;
}
}
},
new UserCredentials('admin', 'changeit')
);
\assert($subscription instanceof VolatileEventStoreSubscription);
echo 'last event number: ' . $subscription->lastCommitPosition() . PHP_EOL;
});