{"id":16772045,"url":"https://github.com/edefimov/async-sockets","last_synced_at":"2025-03-17T02:31:31.320Z","repository":{"id":30682617,"uuid":"34238494","full_name":"edefimov/async-sockets","owner":"edefimov","description":"Library for asynchronous work with sockets based on php streams","archived":false,"fork":false,"pushed_at":"2021-12-08T10:30:26.000Z","size":921,"stargazers_count":43,"open_issues_count":2,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-23T19:38:44.040Z","etag":null,"topics":["async","asynchronous","library","php","sockets"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edefimov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-20T04:46:32.000Z","updated_at":"2024-01-01T18:14:41.000Z","dependencies_parsed_at":"2022-08-21T07:40:28.778Z","dependency_job_id":null,"html_url":"https://github.com/edefimov/async-sockets","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefimov%2Fasync-sockets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefimov%2Fasync-sockets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefimov%2Fasync-sockets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefimov%2Fasync-sockets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edefimov","download_url":"https://codeload.github.com/edefimov/async-sockets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841125,"owners_count":20356440,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","asynchronous","library","php","sockets"],"created_at":"2024-10-13T06:29:46.960Z","updated_at":"2025-03-17T02:31:30.760Z","avatar_url":"https://github.com/edefimov.png","language":"PHP","readme":"Async sockets library\n=====================\n\n[![Build Status](https://img.shields.io/travis/edefimov/async-sockets/master.svg?style=flat)](https://travis-ci.org/edefimov/async-sockets)\n[![Documentation Status](https://readthedocs.org/projects/async-sockets/badge/?version=latest)](http://async-sockets.readthedocs.io/en/latest/?badge=latest)\n[![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/edefimov/async-sockets.svg?style=flat)](https://scrutinizer-ci.com/g/edefimov/async-sockets/)\n[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/c816a980-e97a-46ae-b334-16c6bfd1ec4a.svg?style=flat)](https://insight.sensiolabs.com/projects/c816a980-e97a-46ae-b334-16c6bfd1ec4a)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/edefimov/async-sockets.svg?style=flat)](https://scrutinizer-ci.com/g/edefimov/async-sockets/)\n[![GitHub release](https://img.shields.io/github/release/edefimov/async-sockets.svg?style=flat)](https://github.com/edefimov/async-sockets/releases/latest)\n[![Dependency Status](https://www.versioneye.com/user/projects/55525b5706c318305500014b/badge.png?style=flat)](https://www.versioneye.com/user/projects/55525b5706c318305500014b)\n[![Downloads](https://img.shields.io/packagist/dt/edefimov/async-sockets.svg)](https://packagist.org/packages/edefimov/async-sockets)\n[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.4-777bb4.svg?style=flat)](https://php.net/)\n\nAsync sockets is event-based library for asynchronous work with sockets built on php streams.\n\n## Features\n\n - multiple requests execution at once\n - distinguish frame boundaries\n - server socket support\n - persistent connections support\n - multiple persistent connections to the same host:port\n - processing TLS handshake asynchronous\n - synchronization between sockets\n - determine datagram size for UDP sockets\n - all transports returned by stream_get_transports are supported\n - compatible with symfony event dispatcher component\n - full control over timeouts\n - dynamically adding new request during execution process\n - separate timeout values for each socket\n - custom sockets setup by php stream contexts\n - custom user context for each socket\n - stop request either for certain socket or for all of them\n - strategies for limiting number of running requests\n - error handling is based on exceptions\n - supports libevent engine\n\n## What is it for\nAsync sockets library provides networking layer for applications, hides complexity of I/O operations, \n and cares about connections management. The library will be a powerful base for implementing\n arbitrary networking protocol as for implementing server on PHP. \n Running multiple requests at once decreases delay of I/O operation \n to the size of timeout assigned to the slowest server.\n\n## Documentation\n\n[Stable version](https://async-sockets.readthedocs.io/en/stable/)\n\n[Latest version](https://async-sockets.readthedocs.io/en/latest/)\n\n## Installation\n\nThe recommended way to install async sockets library is through composer\n\nstable version:\n```\n$ composer require edefimov/async-sockets:~0.3.0 --prefer-dist|--prefer-source\n```\n\nactual version:\n```\n$ composer require edefimov/async-sockets:dev-master\n```\n\nUse `--prefer-dist` option in production environment, so as it ignores downloading of test and demo files, \nand `--prefer-source` option for development. Development version includes both test and demo files.\n\n## Quick start\n#### Step 1. Create `AsyncSocketFactory` at point where you want to start request\n```php\n$factory = new AsyncSocketFactory();\n```\n\n#### Step 2. Create RequestExecutor and proper amount of sockets\n```php\n$client        = $factory-\u003ecreateSocket(AsyncSocketFactory::SOCKET_CLIENT);\n$anotherClient = $factory-\u003ecreateSocket(AsyncSocketFactory::SOCKET_CLIENT);\n\n$executor = $factory-\u003ecreateRequestExecutor();\n```\n\n#### Step 3. Create event handler with events, you are interested in\n```php\n$handler = new CallbackEventHandler(\n    [\n        EventType::INITIALIZE   =\u003e [$this, 'onInitialize'],\n        EventType::CONNECTED    =\u003e [$this, 'onConnected'],\n        EventType::WRITE        =\u003e [$this, 'onWrite'],\n        EventType::READ         =\u003e [$this, 'onRead'],\n        EventType::ACCEPT       =\u003e [$this, 'onAccept'],\n        EventType::DATA_ALERT   =\u003e [$this, 'onDataAlert'],\n        EventType::DISCONNECTED =\u003e [$this, 'onDisconnected'],\n        EventType::FINALIZE     =\u003e [$this, 'onFinalize'],\n        EventType::EXCEPTION    =\u003e [$this, 'onException'],\n        EventType::TIMEOUT      =\u003e [$this, 'onTimeout'],\n    ]\n);\n```\n\n#### Step 4. Add sockets into RequestExecutor\n```php\n$executor-\u003esocketBag()-\u003eaddSocket(\n    $client, \n    new WriteOperation(), \n    [\n        RequestExecutorInterface::META_ADDRESS =\u003e 'tls://github.com:443',\n        RequestExecutorInterface::META_CONNECTION_TIMEOUT =\u003e 30,\n        RequestExecutorInterface::META_IO_TIMEOUT =\u003e 5,\n    ],\n    $handler\n);\n$executor-\u003esocketBag()-\u003eaddSocket(\n    $anotherClient, \n    new WriteOperation(), \n    [\n        RequestExecutorInterface::META_ADDRESS =\u003e 'tls://packagist.org:443',\n        RequestExecutorInterface::META_CONNECTION_TIMEOUT =\u003e 10,\n        RequestExecutorInterface::META_IO_TIMEOUT =\u003e 2,\n    ],\n    $handler\n);\n```\n\n#### Step 5. Execute it!\n```php\n$executor-\u003eexecuteRequest();\n```\n\n## Example usage\n### Client socket\n```php\n$factory = new AsyncSocketFactory();\n\n$client        = $factory-\u003ecreateSocket(AsyncSocketFactory::SOCKET_CLIENT);\n$anotherClient = $factory-\u003ecreateSocket(AsyncSocketFactory::SOCKET_CLIENT);\n\n$executor = $factory-\u003ecreateRequestExecutor();\n\n$handler = new CallbackEventHandler(\n    [\n        EventType::INITIALIZE   =\u003e [$this, 'onInitialize'],\n        EventType::CONNECTED    =\u003e [$this, 'onConnected'],\n        EventType::WRITE        =\u003e [$this, 'onWrite'],\n        EventType::READ         =\u003e [$this, 'onRead'],\n        EventType::DISCONNECTED =\u003e [$this, 'onDisconnected'],\n        EventType::FINALIZE     =\u003e [$this, 'onFinalize'],\n        EventType::EXCEPTION    =\u003e [$this, 'onException'],\n        EventType::TIMEOUT      =\u003e [$this, 'onTimeout'],\n    ]\n);\n\n$executor-\u003esocketBag()-\u003eaddSocket(\n    $client, \n    new WriteOperation(), \n    [\n        RequestExecutorInterface::META_ADDRESS =\u003e 'tls://github.com:443',\n        RequestExecutorInterface::META_CONNECTION_TIMEOUT =\u003e 30,\n        RequestExecutorInterface::META_IO_TIMEOUT =\u003e 5,\n    ],\n    $handler\n);\n$executor-\u003esocketBag()-\u003eaddSocket(\n    $anotherClient, \n    new WriteOperation(), \n    [\n        RequestExecutorInterface::META_ADDRESS =\u003e 'tls://packagist.org:443',\n        RequestExecutorInterface::META_CONNECTION_TIMEOUT =\u003e 10,\n        RequestExecutorInterface::META_IO_TIMEOUT =\u003e 2,\n    ],\n    $handler\n);\n\n$executor-\u003eexecuteRequest();\n```\nSee full example [here](https://github.com/edefimov/async-sockets/blob/0.3.0/demos/Demo/RequestExecutorClient.php)\n\n### Server socket\n```php\n$factory       = new AsyncSocketFactory();\n$serverSocket  = $factory-\u003ecreateSocket(AsyncSocketFactory::SOCKET_SERVER);\n$executor      = $factory-\u003ecreateRequestExecutor();\n\n$executor-\u003esocketBag()-\u003eaddSocket(\n    $serverSocket,\n    new ReadOperation(),\n    [\n        RequestExecutorInterface::META_ADDRESS            =\u003e \"tcp://localhost:10280\", // or \"udp://localhost:10280\"\n        RequestExecutorInterface::META_CONNECTION_TIMEOUT =\u003e RequestExecutorInterface::WAIT_FOREVER,\n        RequestExecutorInterface::META_IO_TIMEOUT         =\u003e RequestExecutorInterface::WAIT_FOREVER,\n    ],\n    new CallbackEventHandler(\n        [\n            EventType::ACCEPT =\u003e function (AcceptEvent $event){\n                $event-\u003egetExecutor()-\u003esocketBag()-\u003eaddSocket(\n                    $event-\u003egetClientSocket(),\n                    new ReadOperation(),\n                    [ ],\n                    // client handlers\n                );\n            }\n        ]\n    )\n);\n\n$executor-\u003eexecuteRequest();\n```\nSee full example [here](https://github.com/edefimov/async-sockets/blob/0.3.0/demos/Demo/SimpleServer.php)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedefimov%2Fasync-sockets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedefimov%2Fasync-sockets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedefimov%2Fasync-sockets/lists"}