PHPackages                             amphp/socket - 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. amphp/socket

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

amphp/socket
============

Non-blocking socket connection / server implementations based on Amp and Revolt.

v2.3.1(2y ago)26539.0M—5.3%37[6 issues](https://github.com/amphp/socket/issues)20MITPHPPHP &gt;=8.1CI failing

Since Jul 31Pushed 2w ago12 watchersCompare

[ Source](https://github.com/amphp/socket)[ Packagist](https://packagist.org/packages/amphp/socket)[ Docs](https://github.com/amphp/socket)[ GitHub Sponsors](https://github.com/amphp)[ RSS](/packages/amphp-socket/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (77)Used By (20)

amphp/socket
============

[](#amphpsocket)

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. `amphp/socket` is a library for establishing and encrypting non-blocking sockets. It provides a socket abstraction for clients and servers. It abstracts the really low levels of non-blocking streams in PHP.

[![Latest Release](https://camo.githubusercontent.com/75734eb2c7a0dc2baf04e1012830a13aa8b28f38cdba9d548a4f4055184b3832/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f616d7068702f736f636b65742e7376673f7374796c653d666c61742d737175617265)](https://github.com/amphp/socket/releases)[![MIT License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/amphp/socket/blob/master/LICENSE)

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

[](#installation)

This package can be installed as a [Composer](https://getcomposer.org/) dependency.

```
composer require amphp/socket
```

Requirements
------------

[](#requirements)

`amphp/socket` heavily relies on `amphp/byte-stream`, specifically its `ReadableStream` and `WritableStream` interfaces.

Connecting to a Server
----------------------

[](#connecting-to-a-server)

`amphp/socket` allows clients to connect to servers via TCP, UDP, or Unix domain sockets. You can establish a socket connection using `Amp\Socket\connect()`. It will automatically resolve DNS names and retries other IPs if a connection fails and multiple IPs are available.

```
// You can customize connect() options using ConnectContext
$connectContext = (new Amp\Socket\ConnectContext)
        ->withConnectTimeout(5);

// You can optionally pass a Cancellation object to cancel a pending connect() operation
$deferredCancellation = new Amp\DeferredCancellation();

$socket = connect('amphp.org:80', $connectContext, $deferredCancellation->getCancellation());
```

### Encrypted Connections / TLS

[](#encrypted-connections--tls)

If you want to connect via TLS, use `Amp\Socket\connectTls()` instead or call `$socket->setupTls()` on the returned socket.

### Handling Connections

[](#handling-connections)

`Socket` implements `ReadableStream` and `WritableStream`, so everything from [`amphp/byte-stream`](https://v3.amphp.org/byte-stream) applies for receiving and sending data.

```
#!/usr/bin/env php
