oshitsd/php-socket
Composer 安装命令:
composer require 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.
README 文档
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
oshitsd/php-socket 适用场景与选型建议
oshitsd/php-socket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「websocket」 「Socket」 「laravel」 「Socket.io」 「php-websocket」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oshitsd/php-socket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oshitsd/php-socket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oshitsd/php-socket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
NO LIBRARIES socket per page bridge for your Laravel application.
WebSocket view engine
Free PHP WebSocket
A light weight PHP http client implements PSR7, use socket/curl for sending requests.
Alfabank REST API integration
This is a PHP Framework base on Swoole
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-24