cybercog / yii2-wschat
Online chat based on web sockets and ratchet php
Fund package maintenance!
paypal.me/antonkomarev
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 45
Type:yii2-extension
Requires
- bower-asset/backbone: *
- bower-asset/fontawesome: *
- bower-asset/jquery-cookie: *
- bower-asset/pnotify: *
- bower-asset/underscore: *
- cboden/ratchet: 0.3.*
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: *
- yiisoft/yii2-mongodb: *
Requires (Dev)
This package is auto-updated.
Last update: 2021-01-09 20:16:05 UTC
README
Online chat based on web sockets and ratchet php
Version
0.0.4
Installation
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.
Usage
-
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' ] ]
-
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); $server->run(); }
If you want to use chat for auth users, you must to specify
userClassName
property forChatManager
instance. For example:```php $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:
<?=ChatWidget::widget();?>
or if you want to use chat for auth users just add config as parameter:
<?=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' ]
You can also store added chat, just specify js callback for vent events:
Chat.vent('chat:add', function(chatModel) {
console.log(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.
License
MIT