https://github.com/valtzu/php-websocket-stream-wrapper
PHP Stream wrapper for WebSocket protocol
https://github.com/valtzu/php-websocket-stream-wrapper
php streamwrapper websocket ws wss
Last synced: about 1 year ago
JSON representation
PHP Stream wrapper for WebSocket protocol
- Host: GitHub
- URL: https://github.com/valtzu/php-websocket-stream-wrapper
- Owner: valtzu
- Created: 2023-08-20T21:31:58.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-20T22:42:09.000Z (almost 3 years ago)
- Last Synced: 2023-08-21T00:33:35.421Z (almost 3 years ago)
- Topics: php, streamwrapper, websocket, ws, wss
- Language: PHP
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# WebSocket stream wrapper for PHP 8.1+
## How to use
```php
// All http & tls options work like usual
$context = stream_context_create([
'http' => [...],
'ssl' => [...],
]);
$socket = fopen('wss://ws.postman-echo.com/raw', 'r+', context: $context);
// Non-blocking mode also supported!
// stream_set_blocking($socket, false);
fwrite($socket, 'Hello world');
echo "Received: ", fread($socket, 128), PHP_EOL;
fclose($socket);
```
## How it works internally
Turns out even though you can't open `http` stream wrapper in write mode, you can still write into the socket. So here we use http/tls wrappers for handshake and after that just continue operating on the socket that's left open. See [WebsocketStreamWrapper.php](src/WebsocketStreamWrapper.php) for more.
## Contributing
All contributions are welcome.