• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Non-blocking socket and TLS functionality for PHP based on Amp.

amphp/socket

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 MIT License

Installation

This package can be installed as a Composer dependency.

composer require amphp/socket

Requirements

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

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

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

Handling Connections

Socket implements ReadableStream and WritableStream, so everything from amphp/byte-stream applies for receiving and sending data.

#!/usr/bin/env php
<?php // basic (and dumb) HTTP client

require __DIR__ . '/../vendor/autoload.php';

// This is a very simple HTTP client that just prints the response without parsing.
// league/uri required for this example.

use Amp\ByteStream;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
use League\Uri\Http;
use function Amp\Socket\connect;
use function Amp\Socket\connectTls;

$stdout = ByteStream\getStdout();

if (\count($argv) !== 2) {
    $stdout->write('Usage: examples/simple-http-client.php <url>' . PHP_EOL);
    exit(1);
}

$uri = Http::createFromString($argv[1]);
$host = $uri->getHost();
$port = $uri->getPort() ?? ($uri->getScheme() === 'https' ? 443 : 80);
$path = $uri->getPath() ?: '/';

$connectContext = (new ConnectContext)
        ->withTlsContext(new ClientTlsContext($host));

$socket = $uri->getScheme() === 'http'
        ? connect($host . ':' . $port, $connectContext)
        : connectTls($host . ':' . $port, $connectContext);

$socket->write("GET {$path} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");

ByteStream\pipe($socket, $stdout);

Server

amphp/socket allows listening for incoming TCP connections as well as connections via Unix domain sockets. It defaults to secure TLS settings if you decide to enable TLS.

Listening and Accepting Connections

Use Amp\Socket\Socket\listen() to listen on a port or unix domain socket. It's a wrapper around stream_socket_server that gives useful error message on failures via exceptions.

Once you're listening, accept clients using Server::accept(). It returns a Socket that returns once a new client has been accepted. It's usually called within a while loop:

$server = Socket\listen("tcp://127.0.0.1:1337");

while ($client = $server->accept()) {
    // You shouldn't spend too much time here, because that blocks accepting another client, so we use async():
    async(function () use ($client) {
        // Handle client connection here
    });
}

Handling Connections

Socket implements ReadableStream and WritableStream, so everything from amphp/byte-stream applies for receiving and sending data. It's best to handle clients in their own coroutine, while letting the server accept all clients as soon as there are new clients.

#!/usr/bin/env php
<?php // basic (and dumb) HTTP server

require __DIR__ . '/../vendor/autoload.php';

// This is a very simple HTTP server that just prints a message to each client that connects.
// It doesn't check whether the client sent an HTTP request.

// You might notice that your browser opens several connections instead of just one,
// even when only making one request.

use Amp\Socket;
use function Amp\async;

$server = Socket\listen('127.0.0.1:0');

echo 'Listening for new connections on ' . $server->getAddress() . ' ...' . PHP_EOL;
echo 'Open your browser and visit http://' . $server->getAddress() . '/' . PHP_EOL;

while ($socket = $server->accept()) {
    async(function () use ($socket) {
        $address = $socket->getRemoteAddress();
        $ip = $address->getHost();
        $port = $address->getPort();

        echo "Accepted connection from {$address}." . PHP_EOL;

        $body = "Hey, your IP is {$ip} and your local port used is {$port}.";
        $bodyLength = \strlen($body);

        $socket->write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: {$bodyLength}\r\n\r\n{$body}");
        $socket->end();
    });
}

Closing Connections

Once you're done with a client, close the connection using Socket::close(). If you want to wait for all data to be successfully written before closing the connection, use Socket::end(). See above for an example.

Server Address

Sometimes you don't know the address the server is listening on, e.g. because you listed to tcp://127.0.0.1:0, which assigns a random free port. You can use Server::getAddress() to get the address the server is bound to.

Server Shutdown

Once you're done with the server socket, close the socket. That means, the server won't listen on the specified location anymore. Use Server::close() to close the server socket.

Encrypted Connections / TLS

As already mentioned in the documentation for Amp\Socket\Socket\listen(), you need to enable TLS manually after accepting connections. For a TLS server socket, you listen on the tcp:// protocol on a specified address. After accepting clients, call $socket->setupTls() where $socket is the socket returned from SocketServer::accept().

Warning
Any data transmitted before Socket::setupTls() completes will be transmitted in clear text. Don't attempt to read from the socket or write to it manually. Doing so will read the raw TLS handshake data that's supposed to be read by OpenSSL.

Self-Signed Certificates

There's no option to allow self-signed certificates in ClientTlsContext since it is no more secure than disabling peer verification. To safely use a self-signed certificate, disable peer verification and require fingerprint verification of the certificate using ClientTlsContext::withPeerFingerprint().

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

More Repositories

1

amp

A non-blocking concurrency framework for PHP applications. 🐘
PHP
4,239
star
2

http-server

An advanced async HTTP server library for PHP, perfect for real-time apps and APIs with high concurrency demands.
PHP
1,287
star
3

parallel

An advanced parallelization library for PHP, enabling efficient multitasking, optimizing resource use, and application responsiveness through multiple CPU threads.
PHP
783
star
4

http-client

An advanced async HTTP client library for PHP, enabling efficient, non-blocking, and concurrent requests and responses.
PHP
701
star
5

byte-stream

A non-blocking stream abstraction for PHP based on Amp.
PHP
367
star
6

mysql

An async MySQL client for PHP, optimizing database interactions with efficient non-blocking capabilities. Perfect for responsive, high-performance applications.
PHP
358
star
7

thread

Unmaintained. Use https://github.com/amphp/parallel.
PHP
298
star
8

parallel-functions

Simplified parallel processing for PHP based on Amp.
PHP
271
star
9

ext-fiber

PHP Fiber extension
Assembly
239
star
10

process

An async process dispatcher for Amp.
PHP
229
star
11

ext-uv

C
190
star
12

sync

Non-blocking synchronization primitives for PHP based on Amp and Revolt.
PHP
161
star
13

dns

Async DNS resolution for PHP based on Amp.
PHP
157
star
14

redis

Efficient asynchronous communication with Redis servers, enabling scalable and responsive data storage and retrieval.
PHP
156
star
15

websocket-client

Async WebSocket client for PHP based on Amp.
PHP
144
star
16

parser

A generator parser to make streaming parsers simple.
PHP
124
star
17

websocket-server

WebSocket component for PHP based on the Amp HTTP server.
PHP
114
star
18

serialization

Serialization tools for IPC and data storage in PHP.
PHP
110
star
19

cache

A fiber-aware cache API based on Amp and Revolt.
PHP
99
star
20

file

An abstraction layer and non-blocking file access solution that keeps your application responsive.
PHP
97
star
21

windows-registry

Windows Registry Reader.
PHP
97
star
22

postgres

Async Postgres client for PHP based on Amp.
PHP
96
star
23

hpack

HPack - HTTP/2 header compression implementation in PHP.
PHP
94
star
24

http

HTTP primitives which can be shared by servers and clients.
PHP
88
star
25

beanstalk

Asynchronous Beanstalk Client for PHP.
PHP
65
star
26

cluster

Building multi-core network applications with PHP.
PHP
60
star
27

aerys

A non-blocking HTTP application, WebSocket and file server for PHP based on Amp.
PHP
53
star
28

pipeline

Concurrent iterators and pipeline operations.
PHP
46
star
29

http-server-router

A router for Amp's HTTP Server.
PHP
38
star
30

getting-started

A getting started guide for Amp.
PHP
37
star
31

websocket

Shared code for websocket servers and clients.
PHP
36
star
32

green-thread

PHP
36
star
33

ssh

Async SSH client for PHP based on Amp.
PHP
35
star
34

log

Non-blocking logging for PHP based on Amp and Monolog.
PHP
33
star
35

injector

A recursive dependency injector used to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications.
PHP
31
star
36

uri

Uri Parser and Resolver.
PHP
24
star
37

amphp.github.io

Main website repository.
HTML
24
star
38

react-adapter

Makes any ReactPHP library compatible with Amp.
PHP
24
star
39

artax

An async HTTP/1.1 client for PHP based on Amp.
PHP
23
star
40

http-server-static-content

An HTTP server plugin to serve static files like HTML, CSS, JavaScript, and images effortlessly.
PHP
22
star
41

phpunit-util

Helper package to ease testing with PHPUnit.
PHP
21
star
42

http-server-session

An HTTP server plugin that simplifies session management for your applications. Effortlessly handle user sessions, securely managing data across requests.
PHP
19
star
43

http-server-form-parser

An HTTP server plugin that simplifies form data handling. Effortlessly parse incoming form submissions and extracting its data.
HTML
18
star
44

aerys-reverse

Reverse HTTP proxy handler for Aerys
PHP
16
star
45

mysql-dbal

PHP
16
star
46

sql

Common interfaces for Amp based SQL drivers.
PHP
15
star
47

stomp

A non-blocking STOMP client built on the amp concurrency framework
PHP
15
star
48

loop

Discontinued. Merged into https://github.com/amphp/amp.
PHP
13
star
49

http-tunnel

This package provides an HTTP CONNECT tunnel for PHP based on Amp.
PHP
11
star
50

http-client-psr7

PSR-7 adapter for amphp/http-client.
PHP
10
star
51

http-client-cookies

Automatic cookie handling for Amp's HTTP client.
PHP
10
star
52

rpc

Remote procedure calls for PHP based on Amp.
PHP
9
star
53

http-client-cache

An async HTTP cache for Amp's HTTP client.
PHP
8
star
54

sql-common

Implementations shared by amphp/postgres and amphp/mysql
PHP
7
star
55

php-cs-fixer-config

Common code style configuration for all @amphp projects.
PHP
7
star
56

react-stream-adapter

Adapters to make React's and Amp's streams compatible.
PHP
7
star
57

http-client-guzzle-adapter

PHP
6
star
58

windows-process-wrapper

Child process wrapper to support non-blocking process pipes on Windows.
C
6
star
59

amphp.org

Documentation for AMPHP v3 based libraries.
HTML
6
star
60

quic

PHP
5
star
61

logo

Repository to store the logo and other assets.
3
star
62

dbus

A non-blocking DBus Connector with message serialization based on Amp.
PHP
2
star
63

website-tools

Website administration tools for amphp.org.
PHP
1
star
64

template

This repository serves as template for new amphp projects.
1
star
65

website-shared

Unmaintained. Has been merged into https://github.com/amphp/amphp.github.io.
1
star
66

.github

1
star