oshitsd / php-socket
A PHP package to interact with WebSocket servers. It provides methods to connect, send, receive, and close WebSocket connections, with Laravel integration for seamless usage.
Requires
- php: ^8.0
- textalk/websocket: ^1.5
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.6
README
PhpSocket is a Laravel wrapper for handling Socket.IO WebSocket communication using a simple, expressive API. Built on top of the textalk/websocket
client, it allows your Laravel app to communicate easily with a Socket.IO server.
๐ Features
- Connect to a Socket.IO WebSocket server
- Send and receive messages
- Built-in support for custom payloads and events
- Laravel Facade for simple usage
- Configurable host and port
๐งฐ Requirements
- PHP 8.0 or higher
- Laravel 9, 10, 11, 12 or upper
- WebSocket server with Socket.IO (EIO=4) support
๐ฆ Installation
Install via Composer:
composer require oshitsd/php-socket
Publish the config file:
php artisan vendor:publish --tag=config
This will create a config/php-socket.php
file where you can configure the WebSocket host and port.
โ๏ธ Configuration
In your .env
file, add:
SOCKET_HOST=127.0.0.1 SOCKET_PORT=3000
Or modify the config/php-socket.php
file directly.
๐งช Basic Usage
Connect to Socket
use PhpSocket; PhpSocket::connect();
Send a Message
PhpSocket::send([ "event" => "LARA_NOTIFY", "to" => "all", "message" => [ "time" => date('Y-m-d H:i:s'), "text" => "Laravel says hi ๐", "user" => [ "id" => 1, "name" => "OSHIT SUTRA DAR" ] ] ]);
Receive a Message
$response = PhpSocket::receive();
Close the Connection
PhpSocket::close();
๐งผ Example Output
๐ Connected to socket server successfully. ๐ค Message sent. ๐จ Received: {message} ๐ Connection closed.
๐งช Testing
You can run the package tests with:
./vendor/bin/phpunit
Tests are located in the tests/
directory.
๐ License
This package is open-source software licensed under the MIT license.
๐ Credits
Developed by Oshitsd
WebSocket client powered by textalk/websocket