Web Socket Chat
Online chat based on web sockets and ratchet php, (*1)
![Demo] (doc/demo.gif), (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist fbalabanov/yii2-wschat
or add, (*5)
"fbalabanov/yii2-wschat": "*"
to the require section of your composer.json
file., (*6)
Usage
-
The chat extension can use any database storage supported by yii., (*7)
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., (*8)
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:, (*9)
'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
;, (*10)
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., (*11)
-
To start chat server need to create console command and setup it as demon:, (*12)
-
Create controller which extends yii\console\Controller
:, (*13)
ServerController extends \yii\console\Controller
-
Create action to start server:, (*14)
namespace app\commands;
use fbalabanov\wschat\components\Chat;
use fbalabanov\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:, (*15)
$manager = Yii::configure(new ChatManager(), [
'userClassName' => '\yii\db\ActiveRecord' //allow to get users from MySQL or PostgreSQL
]);
-
To add chat on page just call:, (*17)
<?php echo ChatWidget::widget();?>
or if you want to use chat for auth users just add config as parameter:, (*18)
<?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:, (*19)
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()
., (*20)
If YII_DEBUG
is enabled - all js scripts will be loaded separately., (*21)
Also by default chat will try to load two images:
/avatar_16.png
and /avatar_32.png
from assets folder., (*22)
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:, (*23)
'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., (*24)
License
MIT, (*25)