# ratchet_client **Repository Path**: ganl/ratchet_client ## Basic Information - **Project Name**: ratchet_client - **Description**: No description available - **Primary Language**: Unknown - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-03-15 - **Last Updated**: 2020-12-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ratchet_client (CodeIgniter) CodeIgniter library who allow you to make **powerfull applications** with realtime interactions by using Websocket technology and Ratchetphp ([Socketo.me](http://socketo.me)) 🚀🚀 ## :books: Dependencies - PHP 5.6+ - CodeIgniter Framework (3.1.8+ recommanded) - Composer - PHP Socket extension enabled ## :beginner: Installation ### :arrow_right: Step 1 : Library installation by Composer Just by running following command in the folder of your project : ```sh composer require romainrg/ratchet_client ``` Or by adding following lines to your `composer.json` file : ```json "require": { "romainrg/ratchet_client": "^1.0.0" }, ``` Don't forget to include your autoload to CI config file : ```php $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 * @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 ```php $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 ! - Edit your CI controller `Welcome.php` with the following lines (this will be our server) ```php 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(); } } ``` - Create CI controller `User.php` and add following lines ```php class User extends CI_Controller { public function index($user_id = null) { // We load the CI welcome page with some lines of Javascript $this->load->view('welcome_message', array('user_id' => $user_id)); } } ``` - Edit your CI view `welcome_message.php` with following lines (again :stuck_out_tongue_winking_eye:) ```php Welcome to CodeIgniter

Welcome to CodeIgniter!

``` **Ok you just created your first app !** :heavy_check_mark: (easy with CTRL+C and CTRL+V) #### :arrow_right: Run the Websocket server If you wan't to check you'r work, you have to run the server. Open you'r command prompt then type the command bellow in you'r project folder : ```sh php index.php welcome index ``` If you see the message the message bellow, you are done (don't close your cmd) ! ![First_launch.png](https://user-images.githubusercontent.com/14097222/40981263-d568413a-68da-11e8-9ab2-7b3f7224526e.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](https://user-images.githubusercontent.com/14097222/40725234-2d7ea6aa-6423-11e8-975e-4372125c889d.PNG) You can try is by typing and sending something in each page (see cmd for more logs). ![Cmd_list.png](https://user-images.githubusercontent.com/14097222/40981966-819da07a-68dc-11e8-9717-b0135a107318.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](https://github.com/Textalk/websocket-php) ***(who is included in my composer.json as required library)*** > *Note : The first message is mandatory and always here to perform authentication* ```php $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 : ```php 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; } } ``` - **Auth** type callback is called at first message posted from client. - **Event** type callback is called on every message posted. ## What about Docker :whale: ? Easy to start with this command (php 7.1 used) ```sh 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: - Origin check - WSS support - Add app routing fonctionnality - Websocket native library ## For more CodeIgniter libraries, give me a :beer::grin: [> Beer road](https://www.paypal.me/romaingallien) ## :lock: License [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.html)