GithubHelp home page GithubHelp logo

vektormuhammadlutfi / codeigniter-websocket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from takielias/codeigniter-websocket

0.0 1.0 0.0 39 KB

Codeigniter WebSocket Built for real-time application. Inspired by https://github.com/romainrg/ratchet_client

Home Page: https://cheapapp.net/

License: MIT License

PHP 99.61% HTML 0.39%

codeigniter-websocket's Introduction

CodeIgniter WebSocket Library

CodeIgniter library realtime communication by using Websocket technology and Ratchet (Socketo.me & ratchet_client)

If you Face any problem you may check CodeIgniter WebSocket Example https://github.com/takielias/codeigniter-websocket-example

๐Ÿ“š Dependencies

  • PHP 5.6+
  • CodeIgniter Framework (3.1.* recommanded)
  • Composer
  • PHP sockets extension enabled

๐Ÿ”ฐ Installation

โžก๏ธ Step 1 : Library installation by Composer

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

composer require takielias/codeigniter-websocket

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

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

โžก๏ธ Step 2 : One command Setup

If you want Single command installation just Execute the Command in the Project directory

N.B: It will make 2 new controllers Welcome.php and User.php

php vendor/takielias/codeigniter-websocket/install.php --app_path=application

Here app_path defines your default Codeigniter Application directory Name

one click installation

WOW You made it !!! โœ”๏ธ

Open two pages of your project on following url with different IDs :

http://localhost/your project directory/index.php/user/index/1

http://localhost/your project directory/index.php/user/index/2

โ— In this 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_1

user_2

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

cmd

โžก๏ธ Run the Websocket server Manually

If you want to enable debug mode type the command bellow in you'r project folder :

php index.php welcome index

If you see the message the message bellow, you are done (don't close your cmd) !

First_launch.png

โžก๏ธ Test the App

Broadcast messages with your php App ๐Ÿ’ฅ !

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 โ™ป๏ธ

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/takielias/codeigniter-websocket');
        $this->load->library('Codeigniter_websocket');
        $this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');

        // Run server
        $this->codeigniter_websocket->set_callback('auth', array($this, '_auth'));
        $this->codeigniter_websocket->set_callback('event', array($this, '_event'));
        $this->codeigniter_websocket->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.

Bugs ๐Ÿ› or feature ๐Ÿ’ช

Be free to open an issue or send pull request

Support on Beerpay

Hey dude! Help me out for a couple of ๐Ÿป!

Beerpay Beerpay

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.