fbalabanov / yii2-wschat
Online chat based on web sockets and ratchet php
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- bower-asset/backbone: 1.2.*
- bower-asset/fontawesome: 4.*
- bower-asset/jquery: 2.*
- bower-asset/js-cookie: 2.1.*
- bower-asset/pnotify: 2.0.*
- bower-asset/underscore: 1.8.3
- cboden/ratchet: 0.3.*
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: *
- yiisoft/yii2-mongodb: 2.1.*
Requires (Dev)
This package is not auto-updated.
Last update: 2024-12-22 06:21:15 UTC
README
Online chat based on web sockets and ratchet php
![Demo] (doc/demo.gif)
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist fbalabanov/yii2-wschat
or add
"fbalabanov/yii2-wschat": "*"
to the require section of your composer.json
file.
Usage
-
The chat extension can use any database storage supported by yii.
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.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:'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
;IMPORTANT: if you use db component - you need to create table
history
in your database. The simple examples postgresql and mysql you can see intests/codeception
directory. -
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:
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 forChatManager
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
-
-
To add chat on page just call:
<?php echo ChatWidget::widget();?>
or if you want to use chat for auth users just add config as parameter:
<?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:
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()
.
If
YII_DEBUG
is enabled - all js scripts will be loaded separately.
Also by default chat will try to load two images:
/avatar_16.png
and /avatar_32.png
from assets folder.
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:
'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.
License
MIT