1. Go to this page and download the library: Download oncesk/yii-node-socket library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public function actionIndex() {
// register node socket scripts
// No longer needed - Yii::app()->nodeSocket->registerClientScripts();
}
/**
*
* @method \YiiNodeSocket\Models\Channel getChannel()
* @method \YiiNodeSocket\Frames\Event|null createEvent($name)
*/
class User extends CActiveRecord {
...
public function behaviors() {
return array(
// attach channel behavior
'channel' => array(
'class' => '\YiiNodeSocket\Behaviors\ArChannel',
'updateOnSave' => true
),
// attach subscriber behavior
'subscriber' => array(
'class' => '\YiiNodeSocket\Behaviors\ArSubscriber'
)
);
}
...
}
// Example of subscribe
$user1 = new User();
$user1->setAttributes($attributes);
if ($user1->save()) {
// imagine that $user1->id == 122
// user channel was created and sent to nodejs server
// subscriber was created and sent to nodejs server
// create second user
$user2 = User::model()->findByPk(121);
// now we can subscribe one user to second user
$user1->subscribe($user2);
// and $user2 can catch events from $user1 channel like in twitter
}
// Example of emit event in concrete channel
$user = User::model()->findByPk(122);
if ($user) {
// First method
$event = $user->createEvent('test_event');
if ($event) {
// set event data
$event->setData(array(
'black', 'red', 'white'
));
// send event to user channel
$event->send();
}
// Second method with getting channel
$channel = $user->getChannel();
if ($channel) {
$event = $channel->createEvent('test_event');
// set event data
$event->setData(array(
'black', 'red', 'white'
));
// send event to user channel
$event->send();
}
}
// Example of unsubscribe
$user1 = User::model()->findByPk(122);
$user2 = User::model()->findByPk(121);
$user1->unSubscribe($user2); // now $user2 can not catch events in channel of $user1
// $user - the user model, which can receive this event
$event = Yii::app()->nodeSocket->getFrameFactory()->createUserEventFrame();
$event->setUserId($user->id);
$event->setEventName('message');
$event['text'] = 'Hello, how are you?';
$event->send();
public function actionIndex() {
...
Yii::app()->nodeSocket->registerClientScripts();
...
}
...
// create event frame
$frame = Yii::app()->nodeSocket->getFrameFactory()->createEventFrame();
// set event name
$frame->setEventName('updateBoard');
// set data using ArrayAccess interface
$frame['boardId'] = 25;
$frame['boardData'] = $html;
// or you can use setData(array $data) method
// setData overwrite data setted before
$frame->send();
...
...
// create frame
$frame = Yii::app()->nodeSocket->getFrameFactory()->createPublicDataFrame();
// set key in storage
$frame->setKey('error.strings');
// set data
$frame->setData($errorStrings);
// you can set data via ArrayAccess interface
// $frame['empty_name'] = 'Please enter name';
// set data lifetime
$frame->setLifeTime(3600*2); // after two hours data will be deleted from storage
// send
$frame->send();
...
...
// create frame
$frame = Yii::app()->nodeSocket->getFrameFactory()->createEventFrame();
// set event name
$frame->setEventName('updateBoard');
// set room name
$frame->setRoom('testRoom');
// set data
$frame['key'] = $value;
// send
$frame->send();
...
$invokeFrame = Yii::app()->nodeSocket->getFrameFactory()->createInvokeFrame();
$invokeFrame->invokeFunction('alert', array('Hello world'));
$invokeFrame->send(); // alert will be showed on all clients
...
$product = Product::model()->findByPk($productId);
if ($product) {
$product->price = $newPrice;
if ($product->save()) {
$jFrame = Yii::app()->nodeSocket->getFrameFactory()->createJQueryFrame();
$jFrame
->createQuery('#product' . $product->id)
->find('span.price')
->text($product->price);
$jFrame->send();
// and all connected clients will can see updated price
}
}
...