freelancerua / yii2-chat
Yii2 real-time chat
Installs: 196
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Type:yii2-extension
Requires
- bower-asset/axios: ~0.18.0
- bower-asset/momentjs: ~2.22.2
- bower-asset/socket.io-client: ~2.1.1
- bower-asset/vue: ~2.5.17
- bower-asset/vuex: ~3.0.1
- yiisoft/yii2: ~2.0.0
- yiisoft/yii2-redis: ~2.0.0
This package is auto-updated.
Last update: 2024-04-04 21:29:18 UTC
README
Add to composer.json
"freelancerua/yii2-chat": "@dev"
Configuration
-
Apply migrations. To apply migration set config in console application as following:
'controllerMap' => [ // Migrations for the yii2-chat extension 'migrate-chat' => [ 'class' => \freelancerua\yii2\chat\migrations\MigrationController::class, 'migrationNamespaces' => ['freelancerua\yii2\chat\migrations'], 'migrationPath' => null, 'migrationTable' => 'migration_chat', 'userTable' => '{{%user}}', // Change it if user table has different name ], ],
Now you can run:
./yii migrate-chat/up
-
Setup Redis. For Ubuntu you can follow this instruction: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04
-
Configure yii2-chat extension:
modules' => [ 'chat' => [ 'class' => \freelancerua\yii2\chat\Module::class, 'socketAddress' => 'http://[domain|IP]:[port]', // Required 'userClass' => path\to\user::class, // Required 'onlineWidow' => (60 * 3), // Required (3 minutes or other time) ... ], ],
You can use other module name e.g. yiichat Best way add this configuration to main-local.php (for advanced app)
-
Follow this instructions to update/setup Node.js to version 10.x:
https://github.com/nodesource/distributions#debinstall
-
Install pm2 manager:
npm install pm2 -g
-
In terminal go to
[@vendor]/freelancerua/yii2-chat/node/
-
Update modules:
npm update
-
Copy config.example.json to config.json and setup confg values:
{ "serverPort": 8890, "serverIP": "[IP]", // Exact as your domain or IP set in socketAddress (https required for ssl) "redisPort": 6379, "redisName": "127.0.0.1", "redisAuth": null "ssl": false, "key": "path_to_key", "cert": "path_to_pem" }
-
Start and add a process to the pm2 process list:
pm2 start [@vendor]/freelancerua/yii2-chat/node/server.js --name yii2-chat-server
-
Implement IChatInterface to user class
- getChatImage() & getChatName() depends your class realization
- getIsOnline() & setIsOnline() must be exact as below:
getIsOnline() { return (\freelancerua\yii2\chat\Module::getInstance()->onlineWidow + $this->chat_is_online) > time(); } setIsOnline() { $this->chat_is_online = time(); return $this->update(false); }
If you plan to use this function in not module context do not forget bootstrap this module.
'bootstrap' => [..., 'chat']
Module config options you can change
```
/**
* {@inheritdoc}
*/
public $db = 'db';
/**
* Default chat assets folder
* @var string
*/
public $assets = '@vendor/freelancerua/yii2-chat/assets';
/**
* Default chat style file
* @var string
*/
public $styleFile = 'css/chat.css';
/**
* Default chat js file
* @var string
*/
public $jsFile = 'js/chat.js';
/**
* Format message date when send and update state
* @var string
*/
public $jsDateFormat = 'DD/MM/YYYY H:mm:ss';
/**
* Format message date with PHP
* @var string
*/
public $phpDateFormat = 'php:d/m/Y H:m:s';
/**
* Redis instance name
* @var type
*/
public $redis = 'redis';
/**
* Redis DB host
* @var string
*/
public $redisHost = '127.0.0.1';
/**
* Redis DB port
* @var integer
*/
public $redisPort = 6379;
```