Online chat based on web sockets and ratchet php

Fund package maintenance!

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 45


dev-master 2015-05-10 04:12 UTC

This package is auto-updated.

Last update: 2021-01-09 20:16:05 UTC


Online chat based on web sockets and ratchet php




The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist joni-jones/yii2-wschat "*"

or add

"joni-jones/yii2-wschat": "*"

to the require section of your composer.json file.


  1. Chat use MongoDB and yii2-mongodb extension to store messages history, so you need just specify connection in console config:

    'components' => [
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://username:password@localhost:27017/dbname'
  2. To start chat server need to create console command and setup it as demon:

    • Create controller which extends yii\console\controller:

      ServerController extends \yii\console\controller
    • Create action to start server:

      public function actionRun()
          $server = IoServer::factory(new HttpServer(new WsServer(new Chat(new ChatManager()))), 8080);

    If you want to use chat for auth users, you must to specify userClassName property for ChatManager instance. For example:

     $manager = Yii::configure(new ChatManager(), [
         'userClassName' => '\yii\db\ActiveRecord' //allow to get users from MySQL or PostgreSQL
    • Now, you can run chat server with yii console command:

      yii server/run
  3. To add chat on page just call:


    or if you want to use chat for auth users just add config as parameter:

         'auth' => true,
         'user_id' => '' // setup id of current logged user
     List of available options:
         auth - boolean, default: false
         user_id - mixed, default: null
         port - integer, default: 8080
         chatList - array (allow to set list of preloaded chats), default: [
             id => 1,
             title => 'All'

You can also store added chat, just specify js callback for vent events:

Chat.vent('chat:add', function(chatModel) {

In the callback you will get access to Chat.Models.ChatRoom backbone model.

If YII_DEBUG is enabled - all js scripts will be loaded separately.