定制 baraadark/laravelsocket 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

baraadark/laravelsocket

Composer 安装命令:

composer require baraadark/laravelsocket

包简介

BaraaDark Laravel Socket is a powerful Laravel package that simplifies the integration of real-time WebSocket functionality into your Laravel projects. With this package, you can effortlessly create and manage a Node.js WebSocket server inside your Laravel application and utilize the Elephant.io lib

README 文档

README

BaraaDark Laravel Socket is a powerful Laravel package that simplifies the integration of real-time WebSocket functionality into your Laravel projects. With this package, you can effortlessly create and manage a Node.js WebSocket server inside your Laravel application and utilize the Elephant.io library for seamless communication with Socket.io in PHP

BaraaDark/LaravelSocket Package

Installation

You can install the LaravelSocket package using Composer by running the following command:

composer require baraadark/laravelsocket:dev-master

After installation, configure the server settings by running:

php artisan socket:config

Enter the host and port for Socket event listening.

Next, initialize the Node.js server by running:

php artisan socket:init

This will set up the necessary Node.js dependencies.

Define Socket.IO Events To define Socket.IO events, you need to publish the events.php route file by running:

php artisan vendor:publish --tag=socket-route

Edit the events.php file and define your events logic as Laravel routes. Each route you define here will correspond to two Socket.IO events:

A 'fetch' event for fetching data. A 'load' event for loading data. The event names will match the route name with 'fetch/' or 'load/' prefixed to it. For example:

Route::get('students/index', [SocketController::class, 'index']);

'fetch/students/index' event for fetching data. 'load/students/index' event for loading data. Ensure that your route action returns JSON data using the response()->json() method, as this is required for Socket.IO communication. Example:

return response()->json([
    'data' => $your_data,
    // ... other JSON data
]);

Update Socket.IO Events To update your Socket.IO events based on your routes, run:

php artisan socket:events

This command will generate the necessary JavaScript code to handle the Socket.IO events.

Using LaravelSocket Facade You can use the LaravelSocket Facade object to emit events from anywhere in your code. For example, you can emit events in an observer:

use BaraaDark\LaravelSocket\Facades\LaravelSocket;

class StudentObserver
{
    // ...

    private function reloadData(): void
    {
        LaravelSocket::emit('students/index');
    }

    public function created(Student $student): void
    {
        self::reloadData();
    }

    // ...

    public function forceDeleted(Student $student): void
    {
        self::reloadData();
    }
}

You can also pass data to the emit function:

LaravelSocket::emit('your/route/endpoint', $yourDataAsArray);

This allows you to send specific data to clients when emitting events.

Feel free to customize and expand upon this package to suit your application's real-time communication needs.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-10-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固