Dns Component

Build Status Code Climate

Async DNS resolver for ReactPHP

The main point of the DNS component is to provide async DNS resolution. However, it is really a toolkit for working with DNS messages, and could easily be used to create a DNS server.

Table of contents

Basic usage

The most basic usage is to just create a resolver through the resolver factory. All you need to give it is a nameserver, then you can start resolving names, baby!

$loop = React\EventLoop\Factory::create();
$factory = new React\Dns\Resolver\Factory();
$dns = $factory->create('', $loop);

$dns->resolve('')->then(function ($ip) {
    echo "Host: $ip\n";


See also the first example.

Pending DNS queries can be cancelled by cancelling its pending promise like so:

$promise = $resolver->resolve('');


But there's more.


You can cache results by configuring the resolver to use a CachedExecutor:

$loop = React\EventLoop\Factory::create();
$factory = new React\Dns\Resolver\Factory();
$dns = $factory->createCached('', $loop);

$dns->resolve('')->then(function ($ip) {
    echo "Host: $ip\n";


$dns->resolve('')->then(function ($ip) {
    echo "Host: $ip\n";


If the first call returns before the second, only one query will be executed. The second result will be served from an in memory cache. This is particularly useful for long running scripts where the same hostnames have to be looked up multiple times.

See also the third example.

Custom cache adapter

By default, the above will use an in memory cache.

You can also specify a custom cache implementing CacheInterface to handle the record cache instead:

$cache = new React\Cache\ArrayCache();
$loop = React\EventLoop\Factory::create();
$factory = new React\Dns\Resolver\Factory();
$dns = $factory->createCached('', $loop, $cache);

See also the wiki for possible cache implementations.


The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require react/dns:^0.4.9

More details about version upgrades can be found in the CHANGELOG.


To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit


MIT, see LICENSE file.


  • RFC 1034 Domain Names - Concepts and Facilities
  • RFC 1035 Domain Names - Implementation and Specification