digitv/yii2sockets
Composer 安装命令:
composer require digitv/yii2sockets
包简介
Node.js web-sockets support
关键字:
README 文档
README
Node.js server integration module (web-sockets)
Install with composer
composer require digitv/yii2sockets:1.*
Add console command controller to the config (controllerMap section):
'controllerMap' => [
...
'node-sockets' => '\digitv\yii2sockets\commands\YiiNodeSocketsController',
...
],
Add session component to the 'components' section of main config:
'components' => [
...
'session' => [
'class' => 'digitv\yii2sockets\YiiNodeSocketSession',
'keyPrefix' => 'sess_preffix_',
'timeout' => 600,
],
...
]
It uses Redis DB connection to share session data with Node.js server. Of course you must use redis component to connect Redis DB. Add it in both configs (web and main).
'components' => [
...
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
...
]
And add component do those configs (https not implemented yet):
'components' => [
...
'nodeSockets' => [
'class' => 'digitv\yii2sockets\YiiNodeSocket',
'nodeJsHost' => 'localhost',
'nodeJsPort' => 3001,
'nodeJsScheme' => 'http',
'nodeJsHostClient' => 'your-site.com',
'serviceKey' => 'serviceKeyUsedForCommunication',
'sessionKeyPrefix' => 'sess_preffix_',
'channelsByPermissions' => [
'channel_name' => 'permission',
'channel_name_2' => [
'permission' => '@',
'url' => '/chat/index', //URL on which users can connect to channel
],
],
],
...
]
Parameter channelsByPermissions is an array with channel names and permissions.
If Yii::$app->user->can('permission') returns TRUE than channel channel_name will be added to user automatically.
It runs on component init.
When configs are ready use console command to build Node.js config files:
./yii node-sockets/init
Node.js server
Server is in server subdirectory. Before first run you must install all Node.js modules. Go to server directory and install they with command
npm install
Now you can run server
node app.js
You can use environment variables to overwrite config options. For example:
NODE_ENV=production node app.js
In that case it runs in production mode with disabled debug information.
Messages
There are 3 message classes (I call it Frames):
-
Basic (YiiNodeSocketFrameBasic) used for basic messages.
-
jQuery (YiiNodeSocketFrameJQuery) used for jQuery DOM manipulations.
-
Notify (YiiNodeSocketFrameGrowl) uses
kartik-v/yii2-widget-growlto show messages for user. -
Alert (YiiNodeSocketFrameAlert) plays audio alert to user.
Examples
Send message with array[] body to channel test_channel. On front end will be used Javascript callback jsCallbackName.
Yii::$app->nodeSockets->newMessage()
->setBody([
'id' => $model->id,
'message' => Yii::t('app', 'You have new incoming call'),
])
->setChannel('test_channel')
->setCallback('jsCallbackName')
->send();
Send message to this socket (example is used on AJAX request).
Yii::$app->nodeSockets->newNotify()
->setText('Hello world')
->sendToThis();
Send jQuery frame to user with ID 1. It will remove element with selector #element_selector.
Yii::$app->nodeSockets->newJQuery()
->remove('#element_selector')
->setUser(1)
->send();
Send Alert frame to channel test_channel to all browser windows (not active too):
Yii::$app->nodeSockets->newAlert()
->setAudioId('audio_element_id')
->setChannel('test_channel')
->onlyActiveWindow(false)
->send();
Please notice, that you need to render block with <audio> element in order to play it.
Also includes example YiiNodeSocketFrameChat frame type that sends message depending on its content:
- to
recipient_idandauthor_idif object contains both those properties; - to
channelcomposed automatically otherwise. You can see frame source for details.
$message = new ChatMessage([
'recipient_id' => 4,
'author_id' => 1,
'message' => 'test text message',
]);
Yii::$app->nodeSockets->newChat()
->setMessage($message)
->send();
Javascript callback example:
YiiNodeSockets.callbacks.jsCallbackName = function (message, _socket) {
console.log(message.body);
};
digitv/yii2sockets 适用场景与选型建议
digitv/yii2sockets 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 620 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nodejs」 「node」 「yii2」 「web-sockets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 digitv/yii2sockets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 digitv/yii2sockets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 digitv/yii2sockets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
DNode RPC protocol for PHP 5.3
A custom URL rule class for Yii 2 which allows to create translated URL rules
Send events to a websocket real time engine though PHP
Send events to a websocket real time engine though PHP
SCEditor, a lightweight WYSIWYG BBCode & HTML editor extension for Yii 2.0 Framework
统计信息
- 总下载量: 620
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-25