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: ^7.4 || ^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 7.4 or higher
- Laravel 8, 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=phpsocket-config
This will create a config/phpsocket.php
file where you can configure the WebSocket host and port.
โ๏ธ Configuration
In your .env
file, add:
PHP_SOCKET_ENV=production PHP_SOCKET_HOST=socket.techcanvas.info PHP_SOCKET_API_KEY=demo-tech-canvas-api-key
Or modify the config/phpsocket.php
file directly.
๐งช Basic Usage
๐ Connect to Socket
Without Authentication
use Oshitsd\PhpSocket\Facades\PhpSocket; PhpSocket::connect();
With Authentication
use Oshitsd\PhpSocket\Facades\PhpSocket; $connect = PhpSocket::connect([ "room" => "DEMO_CHAT_ROOM" // Required: Name of the chat room to join "role" => "user", // Optional: User role, defaults to 'user'. Options: 'user' or 'agent' "userId" => 8001, // Optional: Unique user identifier "userName" => "OSHIT SUTRA DAR", // Optional: Display name of the user ]);
๐ค Send a Message
$response = PhpSocket::send([ "event" => "demo_chat", // Required: Name of the event to broadcast "to" => "all", // Target recipient(s). Options: 'all' (broadcast to everyone) or a specific user ID to send a private message. "message" => [ "time" => date('Y-m-d H:i:s'), "text" => "Laravel says hi ๐", "user" => [ "id" => 8001, "name" => "OSHIT SUTRA DAR" ] ] ]);
๐ฉ Receive Message Response
$response = PhpSocket::receiveAck();
๐ Close the Connection
$close = PhpSocket::close();
๐ฃ๏ธ Example Route Usage
You can quickly test sending a message using a simple route in your Laravel application.
use Illuminate\Support\Facades\Route; use Oshitsd\PhpSocket\Facades\PhpSocket; Route::get('send-notification', function () { $connect = PhpSocket::connect([ "room" => "DEMO_CHAT_ROOM", "userId" => 8001, "userName" => "OSHIT SUTRA DAR", ]); $response = PhpSocket::send([ "event" => "demo_chat", "to" => "all", "message" => [ "time" => date('Y-m-d H:i:s'), "text" => "Laravel says hi ๐", "user" => [ "id" => 8001, "name" => "OSHIT SUTRA DAR" ] ] ]); $close = PhpSocket::close(); // Return the connection and message response return response()->json([ 'connect' => $connect, 'response' => $response, 'close' => $close ]); });
When you visit http://your-app-url/send-notification
, this route will:
- Connect to the WebSocket server
- Send a message to all connected clients
- Return the connection and response details as a JSON response
๐ Live Demo
Once you hit the route http://your-app-url/send-notification
, the message will be broadcasted to all connected clients.
You can view the real-time message output here: ๐ Chat App Demo
๐งผ Example Output
๐ Connected to socket server successfully. ๐ค Message sent. ๐จ Received: {message response} ๐ 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 OSHIT SD
WebSocket client powered by textalk/websocket