PHP Websockets with Yii2 integration based on Ratchet.
he preferred way to install this extension is through composer.
- Clone this project in any directory. In this case it will be located in upper level folder
cd ..
git clone https://github.com/BlackBes/yii2-yiisockets.git
- In your project's composer.lock add following lines:
"require": {
...
"blackbes/yii2-yiisockets": "@dev",
...
},
...
"repositories": [
...
{
"type": "path",
"url": "../yii2-yiisockets"
}
...
]
- Run composer install in the project root
composer install
- Add following lines to your config/console.php. Note that provided model should be the one you use to perform authorisation in your app. (Should implement Identity Interface)
'components' => [
...
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\<YourModel>',
'enableAutoLogin' => true
],
...
]
-
Add SocketController.php (you can find template here) to your commands folder
-
Add ecosystem.config.js (you can find template here) to your project root directory
-
Install npm
-
Install pm2
npm i pm2 -g
-
Create sockets folder in your root directory
-
Add controllers that extends blackbes\yiisockets\BaseController
-
Start server
-
Start pm2
Extending this controller will provide following methods that you can use in your websocket controllers:
Method | Parameters | Returns | Example usage |
---|---|---|---|
send() | ConnectionInterface $conn, string $group_id, mixed data | none | $this->send($this->conn, 'get-new-text', ['text' => $text]); |
sendError() | ConnectionInterface $conn, string $error_text | none | --- |
sendToGroupExcludeClient() | string $action, mixed $data, string $group_id, bool $is_json | boolean | $this->sendToGroup('new-message', ['text' => $text], 'chat-'.$chatId); |
sendToGroupExcludeUser() | string $action, mixed $data, string $group_id, bool $is_json | boolean | $this->sendToGroup('new-message', ['text' => $text], 'chat-'.$chatId); |
sendToGroup() | string $action, mixed $data, string $group_id, bool $is_json | boolean | $this->sendToGroup('new-message', ['text' => $text], 'chat-'.$chatId); |
addToGroup() | ConnectionInterface $conn, string $group_id | boolean | $this->addToGroup($this->conn,'chat-'.$chatId); |
removeFromGroup() | ConnectionInterface $conn, string $group_id | boolean | $this->removeFromGroup($this->conn,'chat-'.$chatId); |
isInGroup() | ConnectionInterface $conn, string $group_id | boolean | --- |
GetGroup() | string $group_id | mixed - All connections from specific group | --- |
getClientId() | ConnectionInterface $conn | integer - Id of user | --- |
getData() | string $data_name | mixed - Specified value that you've sent in request | $this->getData('chatId') |
-
Add Yii2WebSockets file to your JavaScript project and import it
import Yii2WebSockets from "path/to/yiisockets-core";
-
Create a variable object login_credentials with following properties:
let login_tokens = { 'login-token': authToken, //Auth token that your identity uses to log in 'connection-type': 'user' };
-
Start WebSocket connection
let _ws = new Yii2WebSockets(login_credentials); _ws.connect(socketAddress, socketPort, socketMode, socketRoute); // Default values are // - socketAddress = 'localhost'; // - socketPort = '8088'; // - socketMode = 'ws'; // - socketRoute = '';
-
Add actions to listen to. Those will trigger when one of your contollers will use a sendToGroup() method which has same action name
let actionToListenTo = 'new-message' //this is for example _ws.addAction(action, function (data) { console.log(data) //here you decide what to do with data, when it arrives });
Note that you can add multiple actions
- Use socketSend method to make requests
let actionName = 'chat/send' // chat - controller name, send - controller's action (actionSend(){}) let additionalData = {'text': "some example text"} // this property you can get in controller by using $this->getData('text') ws.socketSend(actionName, additionalData);
- Process responses in your addAction methods