romainrg / ratchet_client

CodeIgniter library who allow you to make powerfull applications with realtime interactions by using Websocket technology and Ratchetphp 🚀🚀
GNU General Public License v3.0
38 stars 24 forks source link

ratchet_client (CodeIgniter)

CodeIgniter library who allow you to make powerfull applications with realtime interactions by using Websocket technology and Ratchetphp (Socketo.me) 🚀🚀

:books: Dependencies

:beginner: Installation

:arrow_right: Step 1 : Library installation by Composer

Just by running following command in the folder of your project :

composer require romainrg/ratchet_client

Or by adding following lines to your composer.json file :

"require": {
    "romainrg/ratchet_client": "^1.0.0"
},

Don't forget to include your autoload to CI config file :

$config['composer_autoload'] = FCPATH.'vendor/autoload.php';

:arrow_right: Step 2 : Create library config file in your project (Optional)

You have to create in your CI config folder located in ./application/config/ratchet_client.php or the library will take his own config file based on host 0.0.0.0:8282

<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Ratchet Websocket Library: config file
 * @author Romain GALLIEN <romaingallien.rg@gmail.com>
 * @var array
 */
$config['ratchet_client'] = array(
    'host' => '0.0.0.0',    // Default host
    'port' => 8282,         // Default port (be carrefull to set unused server port)
    'auth' => true,         // If authentication is mandatory
    'debug' => true         // Better to set as false in Production
);

:arrow_right: Step 3 : Loading the library

You can add the following lines direclty in your Controller file or your MY_Controller global file

$this->load->add_package_path(FCPATH.'vendor/romainrg/ratchet_client');
$this->load->library('ratchet_client');
$this->load->remove_package_path(FCPATH.'vendor/romainrg/ratchet_client');

You'r almost done :heavy_check_mark:

Examples of use

:arrow_right: Create your first App

It's not very difficult, the library will do your job and much more !

class Welcome extends CI_Controller
{
    public function index()
    {
        // Load package path
        $this->load->add_package_path(FCPATH.'vendor/romainrg/ratchet_client');
        $this->load->library('ratchet_client');
        $this->load->remove_package_path(FCPATH.'vendor/romainrg/ratchet_client');

        // Run server
        $this->ratchet_client->run();
    }
}

First_launch.png

:arrow_right: Test the App

Open three pages of your project on following url with different IDs : http://localhost/myproject/user/index/204 http://localhost/myproject/user/index/402 http://localhost/myproject/user/index/604

:heavy_exclamation_mark: In my example, recipient_id is defined by user_id, as you can see, it's the auth callback who defines recipient ids.

If you have something like that, everything is ok for you:

User_204

You can try is by typing and sending something in each page (see cmd for more logs).

Cmd_list.png

Broadcast messages with your php App :boom: !

If you want to broadcast message with php script or something else you can use library like textalk/websocket (who is included in my composer.json as required library)

Note : The first message is mandatory and always here to perform authentication

$client = new Client('ws://0.0.0.0:8282');

$client->send(json_encode(array('user_id' => 1, 'message' => null)));
$client->send(json_encode(array('user_id' => 1, 'message' => 'Super cool message to myself!')));

Authentication & callbacks :recycle:

The library allow you to define some callbacks, here's an example :

class Welcome extends CI_Controller
{
    public function index()
    {
        // Load package path
        $this->load->add_package_path(FCPATH.'vendor/romainrg/ratchet_client');
        $this->load->library('ratchet_client');
        $this->load->remove_package_path(FCPATH.'vendor/romainrg/ratchet_client');

        // Run server
        $this->ratchet_client->set_callback('auth', array($this, '_auth'));
        $this->ratchet_client->set_callback('event', array($this, '_event'));
        $this->ratchet_client->run();
    }

    public function _auth($datas = null)
    {
        // Here you can verify everything you want to perform user login.
        // However, method must return integer (client ID) if auth succedeed and false if not.
        return (!empty($datas->user_id)) ? $datas->user_id : false;
    }

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

What about Docker :whale: ?

Easy to start with this command (php 7.1 used)

docker run -ti -v C:\Users\my_user\path_to_my_project\:/app -p 8282:8282 -w /app php:7.1-cli sh -c "php index.php welcome index"

Bugs :bug: or feature :muscle:

Be free to open an issue or send pull request

To do :construction:

For more CodeIgniter libraries, give me a :beer::grin:

> Beer road

:lock: License

GNU General Public License v3.0