dev-master
9999999-devBest way to update content on client from server
BSD-3-Clause
The Requires
extension yii2 web socket socket.io client server
Wallogit.com
2017 © Pedro Peláez
Best way to update content on client from server
Best way to send data to client other websocket on yii2 application, (*1)
Dmitriy Khristianov (dkh), (*2)
yii2, npm, node, redis, (*3)
The preferred way to install this extension is through composer., (*4)
Either run, (*5)
php composer.phar require --prefer-dist dkhru/yii2-socket-events "*"
or add, (*6)
"dkhru/yii2-socket-events": "*"
to the require section of your composer.json file., (*7)
Once the extension is installed, cd vendor/dkhru/nodejs and install node modules npm install console-stamp express redis socket.io
Generate SSL certificates for rtserver.
Copy config.js.example to config.js, (*8)
For testing simple run in console node rtserver.js
On linux server use init script example in rtserver.initd, (*9)
In yii2 configure SocketEvent component, (*10)
...
'components'=>[
...
'se'=>[
'class'=>\dkhru\socketEvents\SE::className(),
'socketUrl'=>https://127.0.0.1:8089,
],
...
]
...
Now, you can create you server driven widgets from RegisterSEWidget, (*11)
Simple example:, (*12)
...
class RestWidget extends RegisterSEWidget
{
public $rest;
public function init()
{
if( \Yii::$app->user->isGuest )
throw new ForbiddenHttpException();
$this->object='user';
$this->id=\Yii::$app->user->id;
$restJs=<<<JS
function(data){
if(data['rest']){
el = $('div.rest);
if(el.length)
el.html(data.rest);
}
}
JS;
$this->handlers=[
'rest'=>$restJs // добавляем обработчик
];
parent::init();
}
public function run()
{
parent::run();
return Html::tag('div',$this->rest.'₽',['class'=>'rest']);
}
}
After add widget to view you can update user rest in client browser from server., (*13)
SE::emit('user',$user_id,['handler'=>'rest','data'=>['rest'=>200.00]);
Best way to update content on client from server
BSD-3-Clause
extension yii2 web socket socket.io client server