PHPackages                             edefimov/async-sockets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. edefimov/async-sockets

ActiveLibrary[Queues &amp; Workers](/categories/queues)

edefimov/async-sockets
======================

Library for asynchronous work with sockets based on php streams

0.3.0(9y ago)451.4k—0%4[1 issues](https://github.com/edefimov/async-sockets/issues)[1 PRs](https://github.com/edefimov/async-sockets/pulls)MITPHPPHP &gt;=5.4

Since May 19Pushed 4y ago5 watchersCompare

[ Source](https://github.com/edefimov/async-sockets)[ Packagist](https://packagist.org/packages/edefimov/async-sockets)[ Docs](https://github.com/edefimov/async-sockets)[ RSS](/packages/edefimov-async-sockets/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (3)Versions (9)Used By (0)

Async sockets library
=====================

[](#async-sockets-library)

[![Build Status](https://camo.githubusercontent.com/82dd633acf90fa3e4535d6f8eee040ef56e882fe9b40c1627859d13dd7d99940/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f65646566696d6f762f6173796e632d736f636b6574732f6d61737465722e7376673f7374796c653d666c6174)](https://travis-ci.org/edefimov/async-sockets)[![Documentation Status](https://camo.githubusercontent.com/d13727e1860bb4c90fba96bb7a67539a33f6ba7db72ff3c163aa41350f8e55e4/68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f6173796e632d736f636b6574732f62616467652f3f76657273696f6e3d6c6174657374)](http://async-sockets.readthedocs.io/en/latest/?badge=latest)[![Scrutinizer Coverage](https://camo.githubusercontent.com/1c745f211a280efda204decbb983738075b6143ba20507c185b7093c155b8ef3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f65646566696d6f762f6173796e632d736f636b6574732e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/edefimov/async-sockets/)[![SensioLabsInsight](https://camo.githubusercontent.com/e9f22aa9834ec880e2ea721addfb8ef47eaabc1024f93a4ffa0ac0322d2db89f/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f63383136613938302d653937612d343661652d623333342d3136633662666431656334612e7376673f7374796c653d666c6174)](https://insight.sensiolabs.com/projects/c816a980-e97a-46ae-b334-16c6bfd1ec4a)[![Scrutinizer](https://camo.githubusercontent.com/8814be86b33534f98ebfac063769e82552efafa918e7f26a9de172d2f7e1ece1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f65646566696d6f762f6173796e632d736f636b6574732e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/edefimov/async-sockets/)[![GitHub release](https://camo.githubusercontent.com/a7b9c94a75ab028be79c902c7d50189ba79393b119280d5672be7d221057b82a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f65646566696d6f762f6173796e632d736f636b6574732e7376673f7374796c653d666c6174)](https://github.com/edefimov/async-sockets/releases/latest)[![Dependency Status](https://camo.githubusercontent.com/310041035ee47c2795f20f74e78d98df9db21d1f4b7057674886d1c48fbd4f85/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3535353235623537303663333138333035353030303134622f62616467652e706e673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/55525b5706c318305500014b)[![Downloads](https://camo.githubusercontent.com/d03944f50ef2a053a42fc4dcafc6732eb34d36c5a80f1936daf095227272ad90/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65646566696d6f762f6173796e632d736f636b6574732e737667)](https://packagist.org/packages/edefimov/async-sockets)[![Minimum PHP Version](https://camo.githubusercontent.com/9cf1aacea2867b553e51147e590f4f5e6ef4b9dcd48acf81779a4062fcbeea19/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e342d3737376262342e7376673f7374796c653d666c6174)](https://php.net/)

Async sockets is event-based library for asynchronous work with sockets built on php streams.

Features
--------

[](#features)

- multiple requests execution at once
- distinguish frame boundaries
- server socket support
- persistent connections support
- multiple persistent connections to the same host:port
- processing TLS handshake asynchronous
- synchronization between sockets
- determine datagram size for UDP sockets
- all transports returned by stream\_get\_transports are supported
- compatible with symfony event dispatcher component
- full control over timeouts
- dynamically adding new request during execution process
- separate timeout values for each socket
- custom sockets setup by php stream contexts
- custom user context for each socket
- stop request either for certain socket or for all of them
- strategies for limiting number of running requests
- error handling is based on exceptions
- supports libevent engine

What is it for
--------------

[](#what-is-it-for)

Async sockets library provides networking layer for applications, hides complexity of I/O operations, and cares about connections management. The library will be a powerful base for implementing arbitrary networking protocol as for implementing server on PHP. Running multiple requests at once decreases delay of I/O operation to the size of timeout assigned to the slowest server.

Documentation
-------------

[](#documentation)

[Stable version](https://async-sockets.readthedocs.io/en/stable/)

[Latest version](https://async-sockets.readthedocs.io/en/latest/)

Installation
------------

[](#installation)

The recommended way to install async sockets library is through composer

stable version:

```
$ composer require edefimov/async-sockets:~0.3.0 --prefer-dist|--prefer-source

```

actual version:

```
$ composer require edefimov/async-sockets:dev-master

```

Use `--prefer-dist` option in production environment, so as it ignores downloading of test and demo files, and `--prefer-source` option for development. Development version includes both test and demo files.

Quick start
-----------

[](#quick-start)

#### Step 1. Create `AsyncSocketFactory` at point where you want to start request

[](#step-1-create-asyncsocketfactory-at-point-where-you-want-to-start-request)

```
$factory = new AsyncSocketFactory();
```

#### Step 2. Create RequestExecutor and proper amount of sockets

[](#step-2-create-requestexecutor-and-proper-amount-of-sockets)

```
$client        = $factory->createSocket(AsyncSocketFactory::SOCKET_CLIENT);
$anotherClient = $factory->createSocket(AsyncSocketFactory::SOCKET_CLIENT);

$executor = $factory->createRequestExecutor();
```

#### Step 3. Create event handler with events, you are interested in

[](#step-3-create-event-handler-with-events-you-are-interested-in)

```
$handler = new CallbackEventHandler(
    [
        EventType::INITIALIZE   => [$this, 'onInitialize'],
        EventType::CONNECTED    => [$this, 'onConnected'],
        EventType::WRITE        => [$this, 'onWrite'],
        EventType::READ         => [$this, 'onRead'],
        EventType::ACCEPT       => [$this, 'onAccept'],
        EventType::DATA_ALERT   => [$this, 'onDataAlert'],
        EventType::DISCONNECTED => [$this, 'onDisconnected'],
        EventType::FINALIZE     => [$this, 'onFinalize'],
        EventType::EXCEPTION    => [$this, 'onException'],
        EventType::TIMEOUT      => [$this, 'onTimeout'],
    ]
);
```

#### Step 4. Add sockets into RequestExecutor

[](#step-4-add-sockets-into-requestexecutor)

```
$executor->socketBag()->addSocket(
    $client,
    new WriteOperation(),
    [
        RequestExecutorInterface::META_ADDRESS => 'tls://github.com:443',
        RequestExecutorInterface::META_CONNECTION_TIMEOUT => 30,
        RequestExecutorInterface::META_IO_TIMEOUT => 5,
    ],
    $handler
);
$executor->socketBag()->addSocket(
    $anotherClient,
    new WriteOperation(),
    [
        RequestExecutorInterface::META_ADDRESS => 'tls://packagist.org:443',
        RequestExecutorInterface::META_CONNECTION_TIMEOUT => 10,
        RequestExecutorInterface::META_IO_TIMEOUT => 2,
    ],
    $handler
);
```

#### Step 5. Execute it!

[](#step-5-execute-it)

```
$executor->executeRequest();
```

Example usage
-------------

[](#example-usage)

### Client socket

[](#client-socket)

```
$factory = new AsyncSocketFactory();

$client        = $factory->createSocket(AsyncSocketFactory::SOCKET_CLIENT);
$anotherClient = $factory->createSocket(AsyncSocketFactory::SOCKET_CLIENT);

$executor = $factory->createRequestExecutor();

$handler = new CallbackEventHandler(
    [
        EventType::INITIALIZE   => [$this, 'onInitialize'],
        EventType::CONNECTED    => [$this, 'onConnected'],
        EventType::WRITE        => [$this, 'onWrite'],
        EventType::READ         => [$this, 'onRead'],
        EventType::DISCONNECTED => [$this, 'onDisconnected'],
        EventType::FINALIZE     => [$this, 'onFinalize'],
        EventType::EXCEPTION    => [$this, 'onException'],
        EventType::TIMEOUT      => [$this, 'onTimeout'],
    ]
);

$executor->socketBag()->addSocket(
    $client,
    new WriteOperation(),
    [
        RequestExecutorInterface::META_ADDRESS => 'tls://github.com:443',
        RequestExecutorInterface::META_CONNECTION_TIMEOUT => 30,
        RequestExecutorInterface::META_IO_TIMEOUT => 5,
    ],
    $handler
);
$executor->socketBag()->addSocket(
    $anotherClient,
    new WriteOperation(),
    [
        RequestExecutorInterface::META_ADDRESS => 'tls://packagist.org:443',
        RequestExecutorInterface::META_CONNECTION_TIMEOUT => 10,
        RequestExecutorInterface::META_IO_TIMEOUT => 2,
    ],
    $handler
);

$executor->executeRequest();
```

See full example [here](https://github.com/edefimov/async-sockets/blob/0.3.0/demos/Demo/RequestExecutorClient.php)

### Server socket

[](#server-socket)

```
$factory       = new AsyncSocketFactory();
$serverSocket  = $factory->createSocket(AsyncSocketFactory::SOCKET_SERVER);
$executor      = $factory->createRequestExecutor();

$executor->socketBag()->addSocket(
    $serverSocket,
    new ReadOperation(),
    [
        RequestExecutorInterface::META_ADDRESS            => "tcp://localhost:10280", // or "udp://localhost:10280"
        RequestExecutorInterface::META_CONNECTION_TIMEOUT => RequestExecutorInterface::WAIT_FOREVER,
        RequestExecutorInterface::META_IO_TIMEOUT         => RequestExecutorInterface::WAIT_FOREVER,
    ],
    new CallbackEventHandler(
        [
            EventType::ACCEPT => function (AcceptEvent $event){
                $event->getExecutor()->socketBag()->addSocket(
                    $event->getClientSocket(),
                    new ReadOperation(),
                    [ ],
                    // client handlers
                );
            }
        ]
    )
);

$executor->executeRequest();
```

See full example [here](https://github.com/edefimov/async-sockets/blob/0.3.0/demos/Demo/SimpleServer.php)

###  Health Score

32

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~54 days

Recently: every ~70 days

Total

7

Last Release

3632d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c5c928e3ab2867e6f3f1aed43c9513633dd70fcb1c0aab01a0d4507a68519b9?d=identicon)[edefimov](/maintainers/edefimov)

---

Top Contributors

[![edefimov](https://avatars.githubusercontent.com/u/12027442?v=4)](https://github.com/edefimov "edefimov (168 commits)")

---

Tags

asyncasynchronouslibraryphpsocketsstreamasyncSocket

### Embed Badge

![Health badge](/badges/edefimov-async-sockets/health.svg)

```
[![Health](https://phpackages.com/badges/edefimov-async-sockets/health.svg)](https://phpackages.com/packages/edefimov-async-sockets)
```

###  Alternatives

[react/socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP

1.3k111.4M351](/packages/react-socket)[amphp/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

389113.4M89](/packages/amphp-byte-stream)[icicleio/icicle

Icicle is a PHP library for writing asynchronous code using synchronous coding techniques.

1.1k150.5k14](/packages/icicleio-icicle)[hollodotme/fast-cgi-client

A PHP fast CGI client to send requests (a)synchronously to PHP-FPM.

56423.0M21](/packages/hollodotme-fast-cgi-client)[workerman/phpsocket.io

A server side alternative implementation of socket.io in PHP based on Workerman

2.3k571.7k16](/packages/workerman-phpsocketio)[amphp/parser

A generator parser to make streaming parsers simple.

14950.5M14](/packages/amphp-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
