{"id":19665916,"url":"https://github.com/amphp/websocket-client","last_synced_at":"2025-05-15T11:06:00.857Z","repository":{"id":2994552,"uuid":"48120638","full_name":"amphp/websocket-client","owner":"amphp","description":"Async WebSocket client for PHP based on Amp.","archived":false,"fork":false,"pushed_at":"2024-12-07T02:07:40.000Z","size":281,"stargazers_count":154,"open_issues_count":6,"forks_count":18,"subscribers_count":17,"default_branch":"2.x","last_synced_at":"2025-04-14T19:57:07.894Z","etag":null,"topics":["amphp","async","php","websockets"],"latest_commit_sha":null,"homepage":"https://amphp.org/websocket-client","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/amphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"amphp"}},"created_at":"2015-12-16T16:04:47.000Z","updated_at":"2025-04-10T14:30:24.000Z","dependencies_parsed_at":"2025-02-15T00:13:22.770Z","dependency_job_id":"05b8b5bf-d660-4d36-aea6-53b417bc0452","html_url":"https://github.com/amphp/websocket-client","commit_stats":{"total_commits":198,"total_committers":7,"mean_commits":"28.285714285714285","dds":0.4444444444444444,"last_synced_commit":"24daf3cb8cbf25aa3d16a9434ca72e0af9b005b5"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fwebsocket-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fwebsocket-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fwebsocket-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fwebsocket-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amphp","download_url":"https://codeload.github.com/amphp/websocket-client/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254328385,"owners_count":22052632,"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":["amphp","async","php","websockets"],"created_at":"2024-11-11T16:25:27.610Z","updated_at":"2025-05-15T11:06:00.804Z","avatar_url":"https://github.com/amphp.png","language":"PHP","readme":"# websocket-client\n\nAMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.\n`amphp/websocket-client` provides an asynchronous WebSocket client for PHP based on Amp.\nWebsockets are full-duplex communication channels, which are mostly used for realtime communication where the HTTP request / response cycle has too much overhead.\nThey're also used if the server should be able to push data to the client without an explicit request.\n\nThere are various use cases for a WebSocket client in PHP, such as consuming realtime APIs, writing tests for a WebSocket server, or controlling web browsers via their remote debugging APIs, which are based on WebSockets.\n\n## Installation\n\nThis package can be installed as a [Composer](https://getcomposer.org/) dependency.\n\n```\ncomposer require amphp/websocket-client\n```\n\n## Requirements\n\n- PHP 8.1+\n\n## Usage\n\n### Connecting\n\nYou can create new WebSocket connections using `Amp\\Websocket\\connect()` or calling `connect()` on an instance of `WebsocketConnector`.\nThe `connect()` function accepts a string, PSR-7 `UriInterface` instance, or a `WebsocketHandshake` as first argument. URIs must use the `ws` or `wss` (WebSocket over TLS) scheme.\n\nCustom connection parameters can be specified by passing a `WebsocketHandshake` object instead of a string as first argument, which can also be used to pass additional headers with the initial handshake. The second argument is an optional `Cancellation` which may be used to cancel the connection attempt.\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\nuse function Amp\\Websocket\\Client\\connect;\n\n// Amp\\Websocket\\Client\\connect() uses the WebsocketConnection instance\n// defined by Amp\\Websocket\\Client\\websocketConnector()\n$connection = connect('ws://localhost:1337/websocket');\n\nforeach ($connection as $message) {\n    // $message is an instance of Amp\\Websocket\\WebsocketMessage\n}\n```\n\n#### Custom Connection Parameters\n\nIf necessary, a variety of connection parameters and behaviors may be altered by providing a customized instance of `WebsocketConnectionFactory` to the `WebsocketConnector` used to establish a WebSocket connection.\n\n```php\nuse Amp\\Websocket\\Client\\Rfc6455ConnectionFactory;\nuse Amp\\Websocket\\Client\\Rfc6455Connector;\nuse Amp\\Websocket\\Client\\WebsocketHandshake;\nuse Amp\\Websocket\\ConstantRateLimit;\nuse Amp\\Websocket\\Parser\\Rfc6455ParserFactory;\nuse Amp\\Websocket\\PeriodicHeartbeatQueue;\n\n$connectionFactory = new Rfc6455ConnectionFactory(\n    heartbeatQueue: new PeriodicHeartbeatQueue(\n        heartbeatPeriod: 5, // 5 seconds\n    ),\n    rateLimit: new ConstantRateLimit(\n        bytesPerSecondLimit: 2 ** 17, // 128 KiB\n        framesPerSecondLimit: 10,\n    ),\n    parserFactory: new Rfc6455ParserFactory(\n        messageSizeLimit: 2 ** 20, // 1 MiB\n    ),\n    frameSplitThreshold: 2 ** 14, // 16 KiB\n    closePeriod: 0.5, // 0.5 seconds\n);\n\n$connector = new Rfc6455Connector($connectionFactory);\n\n$handshake = new WebsocketHandshake('wss://example.com/websocket');\n$connection = $connector-\u003econnect($handshake);\n```\n\n### Sending Data\n\nWebSocket messages can be sent using the `Connection::sendText()` and `Connection::sendBinary()` methods.\nText messages sent with `Connection::sendText()` must be valid UTF-8.\nBinary messages send with `Connection::sendBinary()` can be arbitrary data.\n\nBoth methods return as soon as the message has been fully written to the send buffer. This does not mean that the message is guaranteed to have been received by the other party.\n\n### Receiving Data\n\nWebSocket messages can be received using the `Connection::receive()` method. `Connection::receive()` returns a `WebsocketMessage` instance once the client has started to receive a message. This allows streaming WebSocket messages, which might be pretty large. In practice, most messages are rather small, and it's fine buffering them completely by either calling `WebsocketMessage::buffer()` or casting the object to a string. The maximum length of a message is defined by the option given to the `WebsocketParserFactory` instance provided to the `WebsocketConnectionFactory` (10 MiB by default).\n\n```php\nuse Amp\\Websocket\\Client\\WebsocketHandshake;\nuse Amp\\Websocket\\WebsocketCloseCode;\nuse function Amp\\Websocket\\Client\\connect;\n\n// Connects to the websocket endpoint at libwebsockets.org\n// which sends a message every 50ms.\n$handshake = (new WebsocketHandshake('wss://libwebsockets.org'))\n    -\u003ewithHeader('Sec-WebSocket-Protocol', 'dumb-increment-protocol');\n\n$connection = connect($handshake);\n\nforeach ($connection as $message) {\n    $payload = $message-\u003ebuffer();\n\n    printf(\"Received: %s\\n\", $payload);\n\n    if ($payload === '100') {\n        $connection-\u003eclose();\n        break;\n    }\n}\n```\n\n## Versioning\n\n`amphp/websocket-client` follows the [semver](http://semver.org/) semantic versioning specification like all other `amphp` packages.\n\n## Security\n\nIf you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.\n\n## License\n\nThe MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information.\n","funding_links":["https://github.com/sponsors/amphp"],"categories":["Websocket"],"sub_categories":["Tunnel"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fwebsocket-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famphp%2Fwebsocket-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fwebsocket-client/lists"}