If you need create more live application this extension helps you with it.
Features:
-
emit events to all connected clients
-
create rooms and emit events into some room
-
set up data in nodejs memory from php and get in from javascript
-
invoke any function or method of object
-
use JQuery frame for javascript DOM manipulations on client side
Installation see on github project page, link below
javascript:
before use register client scripts
Yii::app()->nodeSocket->registerClientScripts();
var socket = new YiiNodeSocket();
// subscribe on updateBoard event
socket.on('updateBoard', function (data) {
// do any action
});
socket.room('testRoom').join(function (success, numberOfRoomSubscribers) {
// success - boolean, numberOfRoomSubscribers - number of room members
// if error occurred then success = false, and numberOfRoomSubscribers - contains error message
if (success) {
console.log(numberOfRoomSubscribers + ' clients in room: ' + roomId);
// do something
// bind events
this.on('join', function (newMembersCount) {
// fire on client join
});
this.on('data', function (data) {
// fire when server send frame into this room with 'data' event
});
} else {
// numberOfRoomSubscribers - error message
alert(numberOfRoomSubscribers);
}
});
php:
send event
// create event frame
$frame = Yii::app()->nodeSocket->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();
invoke javascript function on all clients from php
$invokeFrame = Yii::app()->nodeSocket->createInvokeFrame();
$invokeFrame->invokeFunction('alert', array('Hello world'));
$invokeFrame->send(); // alert will be showed on all clients
DOM manipulation:
// Update product in your application
$product = Product::model()->findByPk(21);
$product->price = $newPrice;
if ($product->save()) {
// register scripts
Yii::app()->nodeSocket->registerClientScripts();
$jqueryFrame = Yii::app()->nodeSocket->createJQueryFrame();
$jqueryFrame
->createQuery('#product' . $product->id)
->find('span.price')
->text($product->price);
$jqueryFrame->send();
}
Into your javascript
var socket = new YiiNodeSocket();
and price will be updated automatically on all connected clients immediately
sorry, but, this is a first posted extension here and i can not attach github link, only like a text, remove space after https and link will be valid
https ://github.com/oncesk/yii-node-socket