2017 © Pedro Peláez
 

yii2-extension yii2-socket-events

Best way to update content on client from server

image

dkhru/yii2-socket-events

Best way to update content on client from server

  • Wednesday, April 20, 2016
  • by dkhru
  • Repository
  • 3 Watchers
  • 7 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 1 Versions
  • 15 % Grown

The README.md

yii2-socket-event

Best way to send data to client other websocket on yii2 application, (*1)

Author

Dmitriy Khristianov (dkh), (*2)

Require

yii2, npm, node, redis, (*3)

Installation

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)

Configure and run rtserver

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)

Usage

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.'&#8381;',['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]);

The Versions

20/04 2016

dev-master

9999999-dev

Best way to update content on client from server

  Sources   Download

BSD-3-Clause

The Requires

 

extension yii2 web socket socket.io client server