-
Notifications
You must be signed in to change notification settings - Fork 31
/
demo-subscribe-to-stream-from-with-logger.php
87 lines (74 loc) · 2.81 KB
/
demo-subscribe-to-stream-from-with-logger.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
78
79
80
81
82
83
84
85
86
87
<?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\Async\EventStoreCatchUpSubscription;
use Prooph\EventStore\Async\LiveProcessingStartedOnCatchUpSubscription;
use Prooph\EventStore\CatchUpSubscriptionSettings;
use Prooph\EventStore\EndPoint;
use Prooph\EventStore\EventStoreSubscription;
use Prooph\EventStore\ResolvedEvent;
use Prooph\EventStore\SubscriptionDropped;
use Prooph\EventStore\SubscriptionDropReason;
use Throwable;
require __DIR__ . '/../vendor/autoload.php';
Loop::run(function () {
$builder = new ConnectionSettingsBuilder();
$builder->enableVerboseLogging();
$builder->useConsoleLogger();
$connection = EventStoreConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113),
$builder->build()
);
$connection->onConnected(function (): void {
echo 'connected' . PHP_EOL;
});
$connection->onClosed(function (): void {
echo 'connection closed' . PHP_EOL;
});
yield $connection->connectAsync();
yield $connection->subscribeToStreamFromAsync(
'foo-bar',
null,
CatchUpSubscriptionSettings::default(),
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 LiveProcessingStartedOnCatchUpSubscription {
public function __invoke(EventStoreCatchUpSubscription $subscription): void
{
echo 'liveProcessingStarted on ' . $subscription->streamId() . PHP_EOL;
}
},
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;
}
}
}
);
});