Hướng dẫn broadcast php script - phát tập lệnh php

Giới thiệu

Hiện nay trong các ứng dụng web,

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
4 được sử dụng để mang lại các cập nhật đến giao diện người dùng đáp ứng thời gian thực (realtime). Khi dữ liệu được cập nhật trên server, một gói tin sẽ được gửi qua kết nối
         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
4 tới client.server, một gói tin sẽ được gửi qua kết nối
         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
4 tới client.

Xây dựng một ứng dụng như vậy rất dễ dàng với Laravel bằng việc sử dụng

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
6 những event thông qua
         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
4.
         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
8 của Laravel cho phép bạn chia sẻ event giữa server-side code với client-side Javascript code.

Trong bài viết này, tôi sẽ hướng dẫn các bạn cách cài đặt và sử dụng Broadcasting của Laravel thông qua việc sử dụng Laravel Echo.

Chuẩn bị

  • Laravel 5.3+
  • Node 6.0+
  • Redis 3.0+

Cài đặt

Server-side

Các config để sử dụng

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
9 với Laravel đều được lưu trong file

0. Laravel hỗ các broadcast driver như:

1,

2 và

3 cho quá trình phát triển và debugging.

 'default' => env('BROADCAST_DRIVER', 'null'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                //
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

Nếu bạn để giá trị


4 là

5 => bạn sẽ vô hiệu hóa tính năng broadcast.


Trước khi broadcasting bất kỳ event nào, bạn phải đăng ký sử dụng


6 trong file

7. Thông thường provider này sẽ bị comment, bạn chỉ cần tìm đến mảng

8 trong file

7 xóa bỏ comment đi.

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....

composer require predis/predis
0 sẽ cần phải truy cập vào session's CSRF nên bạn cần thêm vào
composer require predis/predis
1 HTML element một thẻ
composer require predis/predis
2 chứa CSRF token:



Cài đặt Broadcast driver

Tôi sử dụng Redis broadcaster nên tôi sẽ cài thêm thư viện Predis:

composer require predis/predis

Cấu hình cho Queue

Trước khi broadcasting event, bạn cần cấu hình và chạy

composer require predis/predis
3. Tất cả event được broadcasting qua queue job sẽ giúp thời giản phản hồi của ứng dụng không bị chậm đi.

Client-side

Laravel không implementation một

composer require predis/predis
4 sẵn nên bạn cần sử dụng một
composer require predis/predis
4 của một bên thứ 3. Laravel khuyến khích bạn sử dụng Laravel Echo Server .


Bạn cần cài đặt package này global

$   npm install -g laravel-echo-server

Sau đó chạy lệnh khởi tạo:

$   laravel-echo-server init
? Do you want to run this server in development mode? Yes
? Which port would you like to serve from? 6001
? Which database would you like to use to store presence channel members? redis
? Enter the host of your Laravel authentication server. http://localhost
? Will you be serving on http or https? http
? Do you want to generate a client ID/Key for HTTP API? Yes
? Do you want to setup cross domain access to the API? Yes
? Specify the URI that may access the API: http://localhost:80
? Enter the HTTP methods that are allowed for CORS: GET, POST
? Enter the HTTP headers that are allowed for CORS: Origin, Content-Type, X-Auth-Token, X-Requested-W
ith, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id

CLI sẽ đưa ra các lựa chọn cho bạn, sau khi hoàn thành, một file laravel-echo-server.json sẽ được tạo ra:

{
	"authHost": "localhost:8005",
	"authEndpoint": "/broadcasting/auth",
	"clients": [
		{
			"appId": "xxx",
			"key": "xxx"
		}
	],
	"database": "redis",
	"databaseConfig": {
		"redis": {
			"port": "6379",
			"host": "localhost"
		},
		"sqlite": {
			"databasePath": "/database/laravel-echo-server.sqlite"
		}
	},
	"devMode": true,
	"host": null,
	"port": "6001",
	"protocol": "http",
	"socketio": {},
	"sslCertPath": "",
	"sslKeyPath": "",
	"sslCertChainPath": "",
	"sslPassphrase": "",
	"apiOriginAllow": {
		"allowCors": false,
		"allowOrigin": "",
		"allowMethods": "",
		"allowHeaders": ""
	}
}

Bạn thực hiện chạy

composer require predis/predis
6 để khởi động Laravel Echo Server

$ laravel-echo-server start

L A R A V E L  E C H O  S E R V E R

version 1.3.6

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

Khi nhận được thông báo như trên, server đã được khởi động, cài đặt thành công.

Tạo event khi có data thay đổi trên server

Chạy command để tạo event:

composer require predis/predis
7, bạn sẽ có một file với nội dung như sau

class ServerCreated implements ShouldBroadcast
{
    use SerializesModels;

    public $user;
    
    public $message;
    
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user, $message)
    {
        $this->user = $user;
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('user.'.$this->user->id);
    }
}

Function

composer require predis/predis
8 sẽ tạo ra channel để client có thể lắng nghe ở đó. Có các tùy chọn
composer require predis/predis
9 khác nhau:

  • $   npm install -g laravel-echo-server
    
    0 đại điện cho kênh public, tất cả các user đều có thể subcribe.
  • $   npm install -g laravel-echo-server
    
    1 ,
    $   npm install -g laravel-echo-server
    
    2 đại diện có các kênh private, phải xác minh trước khi subcribe kênh.

Mặc định Laravel sẽ broadcast sử dụng tên của

$   npm install -g laravel-echo-server
3, nhưng bạn có thể tự định nghĩa tên bằng hàm
$   npm install -g laravel-echo-server
4

public function broadcastAs()
{
    return 'server.created';
}

Hàm

$   npm install -g laravel-echo-server
5 sẽ định nghĩa những dữ liệu nào sẽ được gửi kèm trong các tin

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
0

Vậy là xong, bạn chỉ cần

$   npm install -g laravel-echo-server
6 này ở nơi mong muốn, khi event được phát ra, queue job sẽ tự động broadcast event qua driver broadcast.

Xác thực khi subcribe vào kênh Private

Trong file `routes/channels.php, bạn có thể định nghĩa các xác minh để truy cập kênh

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
1

Lắng nghe sự kiện ở phía client.

Ở dưới client bạn cần cài thêm các package sau:

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
2

Bạn chỉ cần thêm đoạn này vào trang để lắng nghe từ event từ server-side

         /*
         * Application Service Providers...
         */
        ....
        App\Providers\BroadcastServiceProvider::class,
        ....
3

Ở trên mình đang thực hiện subcribe vào channel Private

$   npm install -g laravel-echo-server
7, đối với channel Presence bạn thêm tiền tố
$   npm install -g laravel-echo-server
8 trước tên channel đã tạo ở trên server để có thể subcribe vào kênh.

Kết luận

Như vậy là mình đã giới thiệu về Broadcasting trong Laravel, và thực hiện một ví dụ về tính năng này. Bài viết còn nhiều thiếu sót, hy vọng nhận được góp ý từ mọi người. (bow)

Tham khảo

  • https://medium.com/@adnanxteam/how-to-use-laravel-with-socket-io-e7c7565cc19d
  • https://laravel.com/docs/5.5/broadcasting
  • https://github.com/tlaverdure/laravel-echo-server