承接 erfanvahabpour/laravel-swoole-ws 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

erfanvahabpour/laravel-swoole-ws

Composer 安装命令:

composer require erfanvahabpour/laravel-swoole-ws

包简介

Laravel WebSocket server built on Swoole/OpenSwoole with routing, command protocols, scoped connections, channels, and Redis/Table-backed connection stores.

README 文档

README

A high-performance WebSocket server library for Laravel powered by Swoole / OpenSwoole, designed for:

  • Real-time applications
  • IoT / device communication
  • Command-based device protocols
  • Channel & room broadcasting
  • Scalable multi-connection state management (memory / table / Redis)

This package provides a Laravel-native DX while running outside the HTTP lifecycle.

⚠️ Status: Under active development APIs are stabilizing, but breaking changes may occur.

✨ Features (at a glance)

Core

  • 🚀 Swoole / OpenSwoole WebSocket server
  • 🔌 Laravel service provider & facade
  • 🧭 WebSocket routing (WS::route)
  • 🧠 Command-based device protocol (WS::command, WS::response)
  • 🌐 Handshake URL path scoping (/pub/chat, /attendance, etc.)

Security & Middleware

  • 🔐 Middleware support (ws.auth, custom middleware)
  • 👥 Channels & presence authorization

Messaging

  • 📡 Broadcast to rooms / users
  • 🧩 Scoped command routing

State & Scaling

  • 🗃 Connection stores:

    • In-memory
    • Swoole\Table
    • Redis (multi-server ready)

Tooling

  • ⚙️ Artisan commands (ws:start, ws:stop, ws:reload, ws:list)
  • 🧪 Testbench + PHPUnit support

📦 Requirements

  • PHP 8.2+
  • Laravel 10+
  • Swoole or OpenSwoole extension enabled
php -m | grep swoole
php -m | grep openswoole

📥 Installation

composer require erfanvahabpour/laravel-swoole-ws

Laravel auto-discovery is enabled.

🗂 Publish Config & Routes

php artisan vendor:publish --tag=ws-config
php artisan vendor:publish --tag=ws-routes
php artisan vendor:publish --tag=ws-channels

Creates:

  • config/ws.php
  • routes/ws.php
  • routes/ws_channels.php

▶️ Starting the WebSocket Server

php artisan ws:start
WS server starting on 0.0.0.0:9502

Stop / reload:

php artisan ws:stop
php artisan ws:reload

ℹ️ On every ws:start, the server clears the active connection index to prevent stale connections from appearing in ws:list.

⚙️ Artisan Commands

php artisan ws:start
php artisan ws:stop
php artisan ws:reload
php artisan ws:list

ws:list

Lists active WebSocket connections.

Example:

+---+----+-------------+-----------+------------+
| # | FD | Scope       | User      | Connected  |
+---+----+-------------+-----------+------------+
| 1 | 12 | /pub/chat   | guest     | 2m 14s     |
| 2 | 18 | /attendance| user#42   | 12m 03s    |
+---+----+-------------+-----------+------------+

Options:

php artisan ws:list --count
php artisan ws:list --json

ℹ️ With Swoole\Table, ws:list reflects only the current WS process. Use Redis for cross-process visibility.

🔁 WebSocket Protocols Supported

This library supports two protocols simultaneously.

1️⃣ Legacy Route Protocol (WS::route)

Client → Server

{
  "path": "/chat",
  "action": "send",
  "data": { "text": "hello" },
  "meta": {}
}

Route

WS::route('/chat', 'send', function ($ctx, $data) {
    return ['ok' => true];
});

2️⃣ Command / Device Protocol (WS::command)

Designed for IoT / terminal / attendance devices.

Client → Server

{
  "cmd": "reg",
  "sn": "ABC123",
  "version": "1.0"
}

Server → Client

{
  "ret": "reg",
  "result": true,
  "cloudtime": "2025-01-01 12:00:00"
}

🌐 Handshake Path Scoping

Devices can connect using different URLs:

ws://127.0.0.1:9502/pub/chat
ws://127.0.0.1:9502/attendance

Each path becomes a routing scope, allowing the same command names with different logic.

🧭 Scoped Command Routing

WS::scope('/pub/chat')->command('reg', fn ($ctx, $payload) => ['pong' => true]);

WS::scope('/attendance')->command('reg', function ($ctx, $payload) {
    return [
        'device' => 'attendance',
        'sn' => $payload['sn'] ?? null,
    ];
});

🔄 Automatic Replies

Returning an array automatically sends:

{ "ret": "<cmd>", "result": true, ... }

Manual reply:

$ctx->replyRet('reg', true, ['cloudtime' => now()]);

🔌 Connection Lifecycle Helpers

$ctx->isEstablished();
$ctx->disconnect();
$ctx->disconnectAndForget();

Use disconnectAndForget() for:

  • kicking users
  • invalid devices
  • admin disconnects

🔐 Middleware & Authentication

  • Built-in: ws.auth
  • Handshake token: ?token=TOKEN
  • Message token: { "meta": { "auth": "TOKEN" } }

Custom resolver:

config()->set('ws.auth.resolver', fn ($token) => (object)['id' => 1]);

📡 Channels & Presence

WS::channel('private-chat.{id}', fn ($user, $id) => true);

🗃 Connection Stores

  • memory – simple, single worker
  • table – fast shared memory
  • redis – recommended for scaling & CLI introspection

🧪 Testing & CI

vendor/bin/phpunit
  • PHP 8.2 / 8.3
  • PHPUnit
  • GitHub Actions

🗺 Roadmap

  • WebSocket routing
  • Command protocol
  • Scoped connections
  • Presence broadcasting
  • Rate limiting
  • Metrics endpoint
  • Binary protocol support

License

MIT © Erfan Vahabpour

erfanvahabpour/laravel-swoole-ws 适用场景与选型建议

erfanvahabpour/laravel-swoole-ws 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「php」 「websocket」 「laravel」 「realtime」 「device」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 erfanvahabpour/laravel-swoole-ws 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 erfanvahabpour/laravel-swoole-ws 我们能提供哪些服务?
定制开发 / 二次开发

基于 erfanvahabpour/laravel-swoole-ws 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18