PHPackages                             nikovonlas/yii2sockets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. nikovonlas/yii2sockets

ActiveYii2-extension

nikovonlas/yii2sockets
======================

Node.js web-sockets support for Yii2

1.0.10.0(7y ago)010MITJavaScript

Since Mar 15Pushed 7y ago1 watchersCompare

[ Source](https://github.com/NikoVonLas/yii2sockets)[ Packagist](https://packagist.org/packages/nikovonlas/yii2sockets)[ RSS](/packages/nikovonlas-yii2sockets/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (8)Used By (0)

Node.js server integration module (web-sockets)

**Install with composer**

```
composer require nikovonlas/yii2sockets:1.*

```

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

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

```

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

```
'components' => [
    ...
        'session' => [
            'class' => 'nikovonlas\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' => 'nikovonlas\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

[](#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.
4. Alert (**YiiNodeSocketFrameAlert**) plays audio alert to user.

#### Examples

[](#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()
    ->setOptions([
        'allow_dismiss'     => true,
        'showProgressbar'   => true,
        'placement'         => [
            'from'  => 'bottom',
            'align' => 'right'
        ]
    ])
    ->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 `` element in order to play it.

Also includes example **YiiNodeSocketFrameChat** frame type that sends message depending on its content:

1. to `recipient_id` and `author_id` if object contains both those properties;
2. to `channel` composed 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);
};

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 81.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~81 days

Recently: every ~88 days

Total

7

Last Release

2855d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d2f994afcae6446fa85c0fc2a0f139e0edc469d1bc05c7df98a3fd3fefca29ca?d=identicon)[NikoVonLas](/maintainers/NikoVonLas)

---

Top Contributors

[![digitv](https://avatars.githubusercontent.com/u/11768817?v=4)](https://github.com/digitv "digitv (57 commits)")[![NikoVonLas](https://avatars.githubusercontent.com/u/14359473?v=4)](https://github.com/NikoVonLas "NikoVonLas (13 commits)")

---

Tags

yii2nodejsnodeweb-sockets

### Embed Badge

![Health badge](/badges/nikovonlas-yii2sockets/health.svg)

```
[![Health](https://phpackages.com/badges/nikovonlas-yii2sockets/health.svg)](https://phpackages.com/packages/nikovonlas-yii2sockets)
```

###  Alternatives

[fancyecommerce/fec

fancy ecommerce

2212.0k2](/packages/fancyecommerce-fec)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
