承接 elshiha/laravel-sockeon 相关项目开发

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

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

elshiha/laravel-sockeon

Composer 安装命令:

composer require elshiha/laravel-sockeon

包简介

Laravel integration for Sockeon WebSocket server with artisan commands

README 文档

README

Latest Version on Packagist Total Downloads License

Laravel integration for Sockeon WebSocket server with artisan commands for easy development and deployment.

Features

  • 🚀 Easy Setup - Get WebSocket server running in minutes
  • 🛠️ Artisan Commands - Laravel-style commands for server management
  • 📝 Code Generation - Generate WebSocket controllers with sockeon:make
  • 📊 Log Tailing - Built-in log viewer with sockeon:logs
  • ⚙️ Configurable - Environment-based configuration
  • 🔧 Auto-Discovery - Zero configuration installation

Requirements

  • PHP 8.1 or higher
  • Laravel 11.x or 12.x
  • Sockeon 2.0 or higher

Installation

Install the package via Composer:

composer require elshiha/laravel-sockeon

The package will automatically register itself via Laravel's package auto-discovery.

Publish Configuration

Publish the configuration files:

php artisan vendor:publish --tag=sockeon-config

This creates:

  • config/sockeon.php - Controllers and logging settings
  • config/sockeon-server.php - Full server configuration with CORS, rate limiting, and more

Publish Stubs (Optional)

php artisan vendor:publish --tag=sockeon-stubs

This allows you to customize the controller template used by sockeon:make.

Configuration

Basic Configuration

Add these variables to your .env file:

SOCKEON_HOST=0.0.0.0
SOCKEON_PORT=8080
SOCKEON_DEBUG=false
SOCKEON_LOG_FILE=

Advanced Server Configuration

For advanced features like CORS, rate limiting, authentication, and proxy support, publish the configuration and edit config/sockeon-server.php:

php artisan vendor:publish --tag=sockeon-config

The config/sockeon-server.php file allows you to configure:

Available Options

Basic Settings:

  • host, port, debug - Server fundamentals
  • queue_file - Message persistence
  • auth_key - Authentication key
  • health_check_path - Health check endpoint

CORS Configuration:

  • allowed_origins - Allowed origins (default: ['*'])
  • allowed_methods - HTTP methods
  • allowed_headers - Allowed headers
  • allow_credentials - Allow credentials
  • max_age - Preflight cache duration

Rate Limiting:

  • enabled - Enable/disable rate limiting
  • maxHttpRequestsPerIp - HTTP request limit per IP
  • maxWebSocketMessagesPerClient - WebSocket message limit
  • maxConnectionsPerIp - Connection limit per IP
  • maxGlobalConnections - Global connection limit
  • whitelist - IP whitelist

Security:

  • trust_proxy - Trusted proxy IPs/CIDR ranges

Environment Variables

Option Description Default
SOCKEON_HOST WebSocket server host 0.0.0.0
SOCKEON_PORT WebSocket server port 8080
SOCKEON_DEBUG Enable debug mode false
SOCKEON_LOG_FILE Custom log file path storage/logs/sockeon-websocket.log
SOCKEON_AUTH_KEY Authentication key null
SOCKEON_RATE_LIMIT_ENABLED Enable rate limiting false

Usage

1. Create a WebSocket Controller

Generate a new WebSocket controller:

php artisan sockeon:make ChatController

This creates a controller at app/WebSocket/ChatController.php:

<?php

namespace App\WebSocket;

use Sockeon\Sockeon\Controllers\SocketController;
use Sockeon\Sockeon\WebSocket\Attributes\OnConnect;
use Sockeon\Sockeon\WebSocket\Attributes\OnDisconnect;
use Sockeon\Sockeon\WebSocket\Attributes\SocketOn;

class ChatController extends SocketController
{
    #[OnConnect]
    public function onConnect(int $clientId): void
    {
        echo "Client {$clientId} connected\n";
        
        $this->emit($clientId, 'welcome', [
            'message' => 'Welcome to the server!',
            'clientId' => $clientId
        ]);
    }

    #[OnDisconnect]
    public function onDisconnect(int $clientId): void
    {
        echo "Client {$clientId} disconnected\n";
    }

    #[SocketOn('chat.message')]
    public function handleMessage(int $clientId, array $data): void
    {
        $this->broadcast('chat.message', [
            'from' => $clientId,
            'message' => $data['message'],
            'timestamp' => time()
        ]);
    }
}

2. Register Your Controller

Add your controller to config/sockeon.php:

'controllers' => [
    App\WebSocket\ChatController::class,
],

3. Start the WebSocket Server

php artisan sockeon:serve

Output:

Registered controller: App\WebSocket\ChatController
Starting WebSocket server on ws://0.0.0.0:8080
Press Ctrl+C to stop

4. View Server Logs

Show last 50 lines:

php artisan sockeon:logs

Follow logs in real-time:

php artisan sockeon:logs --follow

Show custom number of lines:

php artisan sockeon:logs --lines=100

5. Refresh (Restart) the Server

Restart the running WebSocket server without manually stopping it:

php artisan sockeon:refresh

This command will:

  • Gracefully stop the currently running server
  • Start a new server instance with the latest configuration
  • Maintain all registered controllers

Output:

Stopping Sockeon server (PID: 12345)...
Server stopped successfully.
Starting new Sockeon server...
Server restarted successfully with new PID: 12346

Note: The refresh command uses a PID file stored at storage/sockeon.pid to track the running server process.

Available Commands

Command Description
sockeon:serve Start the WebSocket server
sockeon:refresh Restart the running WebSocket server
sockeon:logs Display and tail server logs
sockeon:make Generate a WebSocket controller

Client-Side Connection

Connect to your WebSocket server from JavaScript:

// Development
const ws = new WebSocket('ws://localhost:8080');

// Production (behind Nginx proxy)
const ws = new WebSocket('wss://yourdomain.com/sockeon');

ws.onopen = () => {
    console.log('Connected to WebSocket server');
};

ws.onmessage = (event) => {
    const data = JSON.parse(event.data);
    console.log('Received:', data);
};

// Send a message
ws.send(JSON.stringify({
    event: 'chat.message',
    data: { message: 'Hello, World!' }
}));

Production Deployment

For production deployment, you should:

  1. Use a process manager like Supervisor to keep the WebSocket server running
  2. Configure Nginx to proxy WebSocket connections with SSL/WSS
  3. Set proper environment variables in your .env file

Example Supervisor Configuration

[program:sockeon-websocket]
process_name=%(program_name)s
command=php /var/www/your-app/artisan sockeon:serve
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/your-app/storage/logs/sockeon-websocket.log
stopwaitsecs=3600

Example Nginx Configuration

location /sockeon {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_connect_timeout 7d;
    proxy_send_timeout 7d;
    proxy_read_timeout 7d;
    proxy_buffering off;
}

Documentation

For more information about Sockeon:

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

The MIT License (MIT). Please see License File for more information.

Credits

elshiha/laravel-sockeon 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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