Download the PHP package blackbes/yii2-yiisockets without Composer
On this page you can find all versions of the php package blackbes/yii2-yiisockets. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download blackbes/yii2-yiisockets
More information about blackbes/yii2-yiisockets
Files in blackbes/yii2-yiisockets
Package yii2-yiisockets
Short Description PHP Websockets with Yii2 integration based on Ratchet.
License MIT
Informations about the package yii2-yiisockets
Websockets for Yii2
PHP Websockets with Yii2 integration based on Ratchet.
Installation
he preferred way to install this extension is through composer.
1) Clone this project in any directory. In this case it will be located in upper level folder
2) In your project's composer.lock add following lines:
3) Run composer install in the project root
4) 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)
5) Add SocketController.php (you can find template here) to your commands folder
6) Add ecosystem.config.js (you can find template here) to your project root directory
7) Install npm
8) Install pm2
Usage on back
1) Create sockets folder in your root directory
2) Add controllers that extends blackbes\yiisockets\BaseController
4) Start server
5) Start pm2
BaseController Overview
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') |
Connecting to websockets on front
1) Add Yii2WebSockets file to your JavaScript project and import it
2) Create a variable object login_credentials with following properties:
4) Start WebSocket connection
5) Add actions to listen to. Those will trigger when one of your contollers will use a sendToGroup() method which has same action name
Note that you can add multiple actions
Using websockets on front
1) Use socketSend method to make requests
2) Process responses in your addAction methods