Skip to content

Commit 941a29c

Browse files
committed
Documentation for blocking usage
1 parent 13e4f4b commit 941a29c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ as defined in [RFC 6763](http://tools.ietf.org/html/rfc6763).
1919
* [Factory](#factory)
2020
* [createResolver()](#createresolver)
2121
* [Resolver](#resolver)
22+
* [Promises](#promises)
23+
* [Blocking](#blocking)
2224
* [Install](#install)
2325
* [License](#license)
2426

@@ -69,6 +71,8 @@ The [`Factory`](#factory) creates instances of the `React\Dns\Resolver\Resolver`
6971
While React's *normal* DNS resolver uses unicast UDP messages (and TCP streams) to query a given nameserver,
7072
this resolver instance uses multicast UDP messages to query all reachable hosts in your network.
7173

74+
#### Promises
75+
7276
Sending queries is async (non-blocking), so you can actually send multiple DNS queries in parallel.
7377
The mDNS hosts will respond to each DNS query message with a DNS response message. The order is not guaranteed.
7478
Sending queries uses a [Promise](https://github.com/reactphp/promise)-based interface that makes it easy to react to when a query is *fulfilled*
@@ -87,6 +91,45 @@ $resolver->lookup($hostname)->then(
8791

8892
Please refer to the [DNS documentation](https://github.com/reactphp/dns#readme) for more details.
8993

94+
#### Blocking
95+
96+
As stated above, this library provides you a powerful, async API by default.
97+
98+
If, however, you want to integrate this into your traditional, blocking environment,
99+
you should look into also using [clue/block-react](https://github.com/clue/php-block-react).
100+
101+
The resulting blocking code could look something like this:
102+
103+
```php
104+
use Clue\React\Block;
105+
106+
$loop = React\EventLoop\Factory::create();
107+
$factory = new Factory($loop);
108+
$resolver = $factory->createResolver();
109+
110+
$promise = $resolver->lookup('me.local');
111+
112+
try {
113+
$ip = Block\await($promise, $loop);
114+
// IP successfully resolved
115+
} catch (Exception $e) {
116+
// an error occured while performing the request
117+
}
118+
```
119+
120+
Similarly, you can also process multiple lookups concurrently and await an array of results:
121+
122+
```php
123+
$promises = array(
124+
$resolver->lookup('first.local'),
125+
$resolver->lookup('second.local'),
126+
);
127+
128+
$ips = Block\awaitAll($promises, $loop);
129+
```
130+
131+
Please refer to [clue/block-react](https://github.com/clue/php-block-react#readme) for more details.
132+
90133
## Install
91134

92135
The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)

0 commit comments

Comments
 (0)