2017 © Pedro Peláez
 

yii2-extension yii2-wschat

Online chat based on web sockets and ratchet php

image

joni-jones/yii2-wschat

Online chat based on web sockets and ratchet php

  • Wednesday, July 27, 2016
  • by joni-jones
  • Repository
  • 11 Watchers
  • 75 Stars
  • 1,089 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 43 Forks
  • 6 Open issues
  • 9 Versions
  • 1 % Grown

The README.md

Web Socket Chat

Online chat based on web sockets and ratchet php, (*1)

Latest Stable Version Scrutinizer Code Quality Total Downloads License Join the chat at https://gitter.im/joni-jones/yii2-wschat, (*2)

![Demo] (doc/demo.gif), (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

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

or add, (*6)

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

to the require section of your composer.json file., (*7)

Usage

  1. The chat extension can use any database storage supported by yii., (*8)

    If mongodb extension specified the chat will be try to use it as message history storage, otherwise extension will be use specified in application config db component., (*9)

    The simple example how to use mongodb storage is listed below. Install MongoDB and yii2-mongodb extension to store messages history and you need just specify connection in console config:, (*10)

    'components' => [
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://username:password@localhost:27017/dbname'
        ]
    ]
    

    In created mongodb database you need to create collection named as history;, (*11)

    IMPORTANT: if you use db component - you need to create table history in your database. The simple examples postgresql and mysql you can see in tests/codeception directory., (*12)

  2. To start chat server need to create console command and setup it as demon:, (*13)

    • Create controller which extends yii\console\Controller:, (*14)

      ServerController extends \yii\console\Controller
      
    • Create action to start server:, (*15)

      namespace app\commands;
      
      use jones\wschat\components\Chat;
      use jones\wschat\components\ChatManager;
      use Ratchet\Server\IoServer;
      use Ratchet\Http\HttpServer;
      use Ratchet\WebSocket\WsServer;
      
      class ServerController extends \yii\console\Controller
      {
          public function actionRun()
          {
              $server = IoServer::factory(new HttpServer(new WsServer(new Chat(new ChatManager()))), 8080);
              $server->run();
          }
      }
      

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

        $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:, (*17)

      yii server/run
      
  3. To add chat on page just call:, (*18)

    <?php echo ChatWidget::widget();?>
    

    or if you want to use chat for auth users just add config as parameter:, (*19)

    <?php echo ChatWidget::widget([
        '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'
    ],
    add_room - boolean, default: true (allow to user create new chat rooms)

You can also store added chat, just specify js callback for vent events:, (*20)

Chat.vent('chat:add', function(chatModel) {
    console.log(chatModel);
});

This code snipped may be added in your code, but after chat widget loading. In the callback you will get access to Chat.Models.ChatRoom backbone model. Now, you need add your code to save chat room instead console.log()., (*21)

If YII_DEBUG is enabled - all js scripts will be loaded separately., (*22)

Also by default chat will try to load two images: /avatar_16.png and /avatar_32.png from assets folder., (*23)

Possible issues

If you don't see any messages in console log, check flushInterval and exportInterval of your log configuration component. The simple configuration may looks like this:, (*24)

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'flushInterval' => 1,
    'targets' => [
        [
            'class' => 'yii\log\FileTarget',
            'levels' => ['error', 'warning', 'info'],
            'logVars' => [],
            'exportInterval' => 1
        ],
    ],
],

If you use https protocol chat will try to connect to wss instead ws. But Ratchet PHP does not support work via SSL, so you need to use some proxy like stunnel., (*25)

License

MIT, (*26)

The Versions

27/07 2016

dev-master

9999999-dev

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

20/07 2016

1.0.1

1.0.1.0

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

23/06 2016

1.0.0

1.0.0.0

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

10/02 2016

1.0.0-beta

1.0.0.0-beta

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

10/06 2015

0.0.7

0.0.7.0

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

27/05 2015

0.0.6

0.0.6.0

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

13/05 2015

0.0.5

0.0.5.0

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket chat ratchet

10/12 2014

0.0.4

0.0.4.0

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ievgen Sentiabov

extension yii2 backbone websocket ratchet

03/12 2014

dev-yii2-extension-non-amd

dev-yii2-extension-non-amd

Online chat based on web sockets and ratchet php

  Sources   Download

MIT

The Requires

 

by Ievgen Sentiabov

extension yii2 backbone websocket ratchet