PHPackages                             takielias/codeigniter-websocket - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. takielias/codeigniter-websocket

ActiveLibrary[HTTP &amp; Networking](/categories/http)

takielias/codeigniter-websocket
===============================

CodeIgniter Websocket using Ratchet Websocket technology. Inspired by https://github.com/romainrg/ratchet\_client

1.0.4(6y ago)609.1k↓46.7%31[4 issues](https://github.com/takielias/codeigniter-websocket/issues)[1 PRs](https://github.com/takielias/codeigniter-websocket/pulls)MITPHPPHP &gt;=5.4.0

Since Dec 8Pushed 3y ago3 watchersCompare

[ Source](https://github.com/takielias/codeigniter-websocket)[ Packagist](https://packagist.org/packages/takielias/codeigniter-websocket)[ Docs](https://github.com/takielias/codeigniter-websocket) Fund[ GitHub Sponsors](https://github.com/takielias)[ RSS](/packages/takielias-codeigniter-websocket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (6)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/32bb8e8eb9c43141abee99df7dd14bd2e0a36747b189d3c8d56cae398653a237/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74616b69656c6961732f636f646569676e697465722d776562736f636b65742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/takielias/codeigniter-websocket)[![Total Downloads](https://camo.githubusercontent.com/28b96bcfd1604593820eae46943d5d6874b1f36bfc464ea21f2a7083fbea34bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74616b69656c6961732f636f646569676e697465722d776562736f636b65742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/takielias/codeigniter-websocket)

[ ![takielias](https://camo.githubusercontent.com/0cf29a542375e1a46e84d8bf5805a4e5c0a6ee98b6547ccdc0c55eed49d99c69/68747470733a2f2f63646e2e6275796d6561636f666665652e636f6d2f627574746f6e732f76322f64656661756c742d79656c6c6f772e706e67)](https://www.buymeacoffee.com/takielias)

CodeIgniter WebSocket Library
=============================

[](#codeigniter-websocket-library)

CodeIgniter WebSocket library. It allows you to make powerfull realtime applications by using Ratchet ([Socketo.me](http://socketo.me) &amp; [ratchet\_client](https://github.com/romainrg/ratchet_client)) Websocket technology.

WebSocket Library for Codeigniter 4.x
-------------------------------------------------------------------------------------------

[](#websocket-library-for-codeigniter-4x-httpsgithubcomtakieliascodeigniter4-websocket)

If you Face any problem you may check CodeIgniter WebSocket Example

📚 Dependencies
--------------

[](#books-dependencies)

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

🔰 Installation
--------------

[](#beginner-installation)

### ➡️ Step 1 : Library installation by Composer

[](#arrow_right-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

[](#arrow_right-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](https://user-images.githubusercontent.com/38932580/57182660-74df9a80-6ec3-11e9-8b31-37f3fcbf4ccd.png)](https://user-images.githubusercontent.com/38932580/57182660-74df9a80-6ec3-11e9-8b31-37f3fcbf4ccd.png)

**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](https://user-images.githubusercontent.com/38932580/57090224-21851500-6d28-11e9-9321-20d02e146d62.png)](https://user-images.githubusercontent.com/38932580/57090224-21851500-6d28-11e9-9321-20d02e146d62.png)

[![user_2](https://user-images.githubusercontent.com/38932580/57090269-44afc480-6d28-11e9-8ea1-30079a3a47e9.png)](https://user-images.githubusercontent.com/38932580/57090269-44afc480-6d28-11e9-8ea1-30079a3a47e9.png)

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

[![cmd](https://user-images.githubusercontent.com/38932580/57090313-5abd8500-6d28-11e9-8644-8e0323a36a41.png)](https://user-images.githubusercontent.com/38932580/57090313-5abd8500-6d28-11e9-8644-8e0323a36a41.png)

#### ➡️ Run the Websocket server Manually

[](#arrow_right-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](https://user-images.githubusercontent.com/14097222/40981263-d568413a-68da-11e8-9ab2-7b3f7224526e.PNG)](https://user-images.githubusercontent.com/14097222/40981263-d568413a-68da-11e8-9ab2-7b3f7224526e.PNG)

#### ➡️ Test the App

[](#arrow_right-test-the-app)

Broadcast messages with your php App 💥 !
----------------------------------------

[](#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*

```
$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 &amp; callbacks ♻️
---------------------------------

[](#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/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.

How to receive response into Codeigniter Controller ?
=====================================================

[](#how-to-receive-response-into-codeigniter-controller-)

Please look at the Welcome.php 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->set_callback('roomleave', array($this, '_roomleave'));
	$this->codeigniter_websocket->run();
}

public function _roomleave($data = null)
{
	// Here you will receive data from the frontend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

```

The main concept is the callback function.

You would receive the response into the defined function. You can trigger the event from the front end like below using jQuery

```
    socket.send(JSON.stringify({
        'type': 'roomleave',
        'room_name': targetName,
        'user_id': "buzz4rd"
    }));

```

It would trigger the function below

```
public function _roomleave($data = null)
{
	// Here you will receive data from fron tend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

```

You May Check [Room Chat using PHP websocket](https://github.com/takielias/ci-socket-chat). It was built using [this](https://github.com/romainrg/ratchet_client)

Bugs 🐛 or feature 💪
-------------------

[](#bugs-bug-or-feature-muscle)

Be free to open an issue or send pull request

Support on Buy Me A Coffee
--------------------------

[](#support-on-buy-me-a-coffee)

Hey dude! Help me out for a cup of ☕!

[ ![takielias](https://camo.githubusercontent.com/0cf29a542375e1a46e84d8bf5805a4e5c0a6ee98b6547ccdc0c55eed49d99c69/68747470733a2f2f63646e2e6275796d6561636f666665652e636f6d2f627574746f6e732f76322f64656661756c742d79656c6c6f772e706e67)](https://www.buymeacoffee.com/takielias)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~86 days

Total

5

Last Release

2375d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/fad247d1f93a5e71551ddb8719214e25ebbc427726d50359e243b7f4f2aacbfc?d=identicon)[takielias](/maintainers/takielias)

---

Top Contributors

[![takielias](https://avatars.githubusercontent.com/u/38932580?v=4)](https://github.com/takielias "takielias (25 commits)")

---

Tags

broadcast-messagescodeigniter-librarycodeigniter-websocketphp-websocketratchetrealtime-messagingwebsocketwebsocketscodeigniterwebsocketrealtimeRatchetPHP7PHP Librarycodeigniter websocket

### Embed Badge

![Health badge](/badges/takielias-codeigniter-websocket/health.svg)

```
[![Health](https://phpackages.com/badges/takielias-codeigniter-websocket/health.svg)](https://phpackages.com/packages/takielias-codeigniter-websocket)
```

###  Alternatives

[takielias/codeigniter4-websocket

Websocket using Ratchet Websocket technology for CodeIgniter 4

883.0k](/packages/takielias-codeigniter4-websocket)[cboden/ratchet

PHP WebSocket library

6.4k21.4M239](/packages/cboden-ratchet)[romainrg/ratchet_client

CodeIgniter library who allow you to make powerfull applications with realtime interactions by using Websocket technology

396.6k](/packages/romainrg-ratchet-client)[gos/web-socket-bundle

Symfony Web Socket Bundle

6202.2M8](/packages/gos-web-socket-bundle)[ratchet/pawl

Asynchronous WebSocket client

6148.9M214](/packages/ratchet-pawl)[centrifugal/phpcent

PHP library to communicate with Centrifugo HTTP API

1822.3M4](/packages/centrifugal-phpcent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
