PHP code example of takielias / codeigniter4-websocket

1. Go to this page and download the library: Download takielias/codeigniter4-websocket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

takielias / codeigniter4-websocket example snippets


$routes->setAutoRoute(true);
sh
php spark websocket:publish
sh
php spark serve
sh
PHP spark serve --port=9092
sh
php public/index.php Websocket start
sh
    public function start()
    {
        $ws = service('CodeigniterWebsocket');
        $ws->set_callback('auth', array($this, '_auth'));
        $ws->set_callback('event', array($this, '_event'));
        $ws->run();
    }

    public function _auth($datas = null)
    {
        // Here you can verify everything you want to perform user login.

        return (!empty($datas->user_id)) ? $datas->user_id : false;
    }

    public function _event($datas = null)
    {
        // Here you can do everything you want, each time message is received 
        echo 'Hey ! I\'m an EVENT callback' . PHP_EOL;
    }