PHPackages                             amphp/websocket-client - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. amphp/websocket-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

amphp/websocket-client
======================

Async WebSocket client for PHP based on Amp.

v2.0.2(8mo ago)1623.0M—8.4%16[6 issues](https://github.com/amphp/websocket-client/issues)20MITPHPPHP &gt;=8.1CI failing

Since Nov 16Pushed 8mo ago16 watchersCompare

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

READMEChangelog (10)Dependencies (16)Versions (20)Used By (20)

websocket-client
================

[](#websocket-client)

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. `amphp/websocket-client` provides an asynchronous WebSocket client for PHP based on Amp. Websockets are full-duplex communication channels, which are mostly used for realtime communication where the HTTP request / response cycle has too much overhead. They're also used if the server should be able to push data to the client without an explicit request.

There 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.

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

[](#installation)

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

```
composer require amphp/websocket-client

```

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

[](#requirements)

- PHP 8.1+

Usage
-----

[](#usage)

### Connecting

[](#connecting)

You can create new WebSocket connections using `Amp\Websocket\connect()` or calling `connect()` on an instance of `WebsocketConnector`. The `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.

Custom 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.

```
