PHPackages                             immusen/yii2-swoole-websocket - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. immusen/yii2-swoole-websocket

ActiveYii2-extension[HTTP &amp; Networking](/categories/http)

immusen/yii2-swoole-websocket
=============================

Websocket server for Yii2 base on swoole 4, Support JSONRPC, Resolve 'method' as a route reflect into controller/action, And support http or redis pub/sub to trigger async task from your web application.

v1.11(6y ago)338.8k↓100%12[2 issues](https://github.com/immusen/yii2-swoole-websocket/issues)[3 PRs](https://github.com/immusen/yii2-swoole-websocket/pulls)Apache-2.0PHP

Since Feb 26Pushed 6y ago3 watchersCompare

[ Source](https://github.com/immusen/yii2-swoole-websocket)[ Packagist](https://packagist.org/packages/immusen/yii2-swoole-websocket)[ RSS](/packages/immusen-yii2-swoole-websocket/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (1)Versions (4)Used By (0)

Websocket rpc server For Yii2 Base On Swoole 4
==============================================

[](#websocket-rpc-server-for-yii2-base-on-swoole-4)

Websocket server for Yii2 base on swoole 4, Support JSON-RPC, Resolve 'method' as a route reflect into controller/action, And support http or redis pub/sub to trigger async task from your web application.

Installation
------------

[](#installation)

Install Yii2: [Yii2](https://www.yiiframework.com).

Install swoole: [swoole](https://www.swoole.com), recommend version 4+.

Other dependency: php-redis extension.

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist immusen/yii2-swoole-websocket "~1.0"

```

or add

```
"immusen/yii2-swoole-websocket": "~1.0"

```

to the require section of your `composer.json` file.

Test or Usage
-------------

[](#test-or-usage)

> after installation, cd project root path, e.g. cd yii2-advanced-project/

```
mv vendor/immusen/yii2-swoole-websocket/example/websocket ./
mv vendor/immusen/yii2-swoole-websocket/example/websocket-server ./
chmod a+x ./websocket-server

```

> run:

```
./websocket-server start    //start server
./websocket-server restart  //restart server
./websocket-server reload  //reload task worker
./websocket-server status  //server status

```

> config :

```
vim ./websocket/config/params.php

```

> or coding in ./websocket/controllers/

Example:
--------

[](#example)

Chat room demo, code: [./example/websocket/controllers/RoomController.php](https://github.com/immusen/yii2-swoole-websocket/blob/master/example/websocket/controllers/RoomController.php)

> client join room: websocket client send:

```
  {
      "jsonrpc":"2.0",
      "id":1,
      "method":"room/join",
      "params":{
          "id":"100111",
          "info":{
              "age":"19",
              "gender":"f"
          }
      }
  }

```

> the websocket client which had joined same room (id:100111) will get message like this:

```
  {
      "jsonrpc":"2.0",
      "id":1,
      "result":{
          "type":"join",
          "count":85,
          "info":{
              "age":"19",
              "gender":"f"
          }
      }
  }

```

> chat message websocket client send:

```
    {
        "jsonrpc":"2.0",
        "id":1,
        "method":"room/msg",
        "params":{
            "id":"100111",
            "content":{
                "text":"Hello world!"
            }
        }
    }

```

> this room member will get:

```
    {
        "jsonrpc":"2.0",
        "id":1,
        "result":{
            "text":"Hello world!"
        }
    }

```

Coding:
-------

[](#coding)

1, Create Controller under websocket/controllers, or other path which defined with "controllerNamespace" in websocket/config/main.php

```
