dejmon/yii2sockets

Node.js web-sockets support

Installs: 54

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 4

Open Issues: 0

Language:JavaScript

Type:yii2-extension

dev-master / 1.x-dev 2017-03-31 13:05 UTC

This package is not auto-updated.

Last update: 2024-05-11 18:13:04 UTC


README

It's Node.js server integration module (web-sockets) based on digitv/yii2sockets

Install with composer

composer require dejmon/yii2sockets:1.x-dev

Add console command controller to the config (controllerMap section):

'controllerMap' => [
    ...
    'node-sockets' => '\dejmon\yii2sockets\commands\YiiNodeSocketsController',
    ...
],

Add session component to the 'components' section of main config:

'components' => [
    ...
        'session' => [
            'class' => 'dejmon\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' => 'dejmon\yii2sockets\YiiNodeSocket',
            'nodeJsHost' => 'localhost',
            'nodeJsPort' => 3001,
            'nodeJsScheme' => 'http',
            'nodeJsHostClient' => 'your-site.com',
            'serviceKey' => 'serviceKeyUsedForCommunication',
            'sessionKeyPrefix' => 'sess_preffix_',
            'channelsByPermissions' => [
                'channel_name' => 'permission',
            ],
        ],
    ...
]

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 session 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):

  1. Basic (YiiNodeSocketFrameBasic) used for basic messages.

  2. jQuery (YiiNodeSocketFrameJQuery) used for jQuery DOM manipulations.

  3. Notify (YiiNodeSocketFrameGrowl) uses kartik-v/yii2-widget-growl to show messages for 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();

Javascript callback example:

YiiNodeSockets.callbacks.jsCallbackName = function (message, _socket) {
    console.log(message.body);
};