takielias/codeigniter-websocket
Composer 安装命令:
composer require takielias/codeigniter-websocket
包简介
CodeIgniter Websocket using Ratchet Websocket technology. Inspired by https://github.com/romainrg/ratchet_client
README 文档
README
CodeIgniter WebSocket Library
CodeIgniter WebSocket library. It allows you to make powerfull realtime applications by using Ratchet (Socketo.me & ratchet_client) Websocket technology.
WebSocket Library for Codeigniter 4.x https://github.com/takielias/codeigniter4-websocket
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
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:
You can try typing and sending something in each page (see cmd for more logs).
➡️ 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) !
➡️ 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.
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. It was built using this
Bugs 🐛 or feature 💪
Be free to open an issue or send pull request
Support on Buy Me A Coffee
Hey dude! Help me out for a cup of ☕!
takielias/codeigniter-websocket 适用场景与选型建议
takielias/codeigniter-websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.36k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2018 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「websocket」 「codeigniter」 「Ratchet」 「realtime」 「PHP7」 「php-library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 takielias/codeigniter-websocket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 takielias/codeigniter-websocket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 takielias/codeigniter-websocket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 9.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 63
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-08



