PHP Framework websocket Active Message Push

Laravel combines swoole to implement a function that actively triggers message pushing, which allows you to send custom messages to all members of the template message, instead of sending messages through the client, and listens to the messages sent in the message on the service side for business logic.

Active Message Push Implementation
Normally we use swoole to write WebSocket services that are likely to use the three listening states open,message,close most often, but in case we don't look at the use of the onRequest callback below, yes, to solve this active message push, we need to use the onRequest callback.
Official Document: Because swoole_websocket_server inherits from swoole_http_server, there is an onRequest callback in the websocket.

Detailed implementation:

# Here is a Commands in laravel
# Run php artisan swoole start to run
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use swoole_websocket_server;

class Swoole extends Command
{
    public $ws;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'swoole {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Active Push Message';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $arg = $this->argument('action');
        switch ($arg) {
            case 'start':
                $this->info('swoole server started');
                $this->start();
                break;
            case 'stop':
                $this->info('swoole server stoped');
                break;
            case 'restart':
                $this->info('swoole server restarted');
                break;
        }
    }

    /**
     * Start Swoole
     */
    private function start()
    {
        $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
        //Listen for WebSocket Connection Open Events
        $this->ws->on('open', function ($ws, $request) {
        });
        //Listen for WebSocket message events
        $this->ws->on('message', function ($ws, $frame) {
            $this->info("client is SendMessage\n");
        });
        //Listen for WebSocket Active Push Message Events
        $this->ws->on('request', function ($request, $response) {
            $scene = $request->post['scene'];       // Get value
            $this->info("client is PushMessage\n".$scene);
        });
        //Listen for WebSocket Connection Close Events
        $this->ws->on('close', function ($ws, $fd) {
            $this->info("client is close\n");
        });
        $this->ws->start();
    }
}

Previously, the implementation of onRequest in swoole was discussed. The following implementation actively triggers the onRequest callback in the controller.The implementation is a curl request that we are familiar with.

# After calling the activepush method, it will print out in cmd 
# Cliis PushMessage Actively Pushes Message Terms
    /**
     * CURL request
     * @param $data
     */
    public function curl($data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "http://127.0.0.1:9502");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_exec($curl);
        curl_close($curl);
    }
    
    /**
     * Active Trigger
     */
    public function activepush()
    {
        $param['scene'] = 'Active push message';
        $this->curl($param);            // Active push message

purpose
The onRequest callback is particularly useful for push messages that need to be invoked in the controller, such as template messages, which are invoked in the controller.

There are always some problems and bottlenecks when you're advanced. You don't know where to start without a sense of direction when you write more business code. I've sorted some data about it, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell Scripting, Docker, Micro Services, Nginx and other knowledge points Advanced dry goods needs can be shared free of charge to everyone, needs Stamp here, please

Keywords: PHP curl Laravel Redis

Added by neptun on Tue, 12 Nov 2019 05:17:24 +0200