Download the PHP package oncesk/yii-node-socket without Composer
On this page you can find all versions of the php package oncesk/yii-node-socket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download oncesk/yii-node-socket
More information about oncesk/yii-node-socket
Files in oncesk/yii-node-socket
Package yii-node-socket
Short Description Реализует связь между php и client javascript по средству сокет соединения.
License
Informations about the package yii-node-socket
Yii Node Socket
Connect php, javascript, nodejs in one Yii application.
Yii1
Hi, if you need work with yii1 you can do it from (https://github.com/oncesk/yii-node-socket/tree/2.0.0)
What you can do:
- send event(s) to all clients or in concrete room or in concrete channel
- now you can send events to single (concrete user) by user id!
- call some function or object method in window context
- you can change DOM model with jquery from php
- ability to set up data and get it in your javascript application
- send events from javascript for all clients or clients in concrete room or channel
Changes
- Updated for Yii 2.0
- Added Namespacing Updated for Yii 2.0
- Commposer settings updated to work with yii extension updates
Requirements
- linux/unix/windows
- git
- vps or dedicated server (for nodejs process)
- curl has to be installed or enabled
Installation
Install nodejs, if not installed see http://nodejs.org/
Install extension
-
Composer
- Using git clone
Now go to the folder where you install extension application.ext.yii-node-socket and execute
Yii configuration
-
Configure console command in (console/config/main.php). You can use config below:
- Register Yii component, need to add into frontend/config/main.php in your frontend application:
Notice: host should be a domain name like in you virtual host configuration or server ip address if you request page using ip address
- Configure aliases in the common config in (common/config/main.php).
- The first is for Yii to find the PHP Namespace and the second is to find the JS assets.
- You can use config below:
Notice: if you will be use behaviors or node-socket models, you need to add nodeSocket component in preload components list
Install nodejs components in application.ext.yii-node-socket.lib.js.server:
If you get errors issuing this command try the following:
Congratulation, installation completed!
Notice: if the name of the component will not be nodeSocket, your need to use special key in console command --componentName=component_name
Console command actions
Use (./yiic node-socket)
Definitions
- Frame - data package for nodejs server wrapped into Class. Per one request to nodejs server you can send only 1 frame. For send several frames at a time use Multiple frame.
- room - one or more clients in concrete namespace: every client can create room, other clients can join into concrete room, any client in room can send event in this room.
Javascript
Before use in javascript, register client stripts like here Depricated in Yii 2.0 - The Asset Manager registers the files on demand.
Events
Work in javascript
Use YiiNodeSocket
class
Start work
Catch Events
Now events can be created only on PHP side. All data transmitted in json format. Into callback function data was pasted as javascript native object (or string, integer, depends of your PHP Frame config)
Rooms
Channels
Channel is very similar to the room, but you can controll access to channel for clients
Emit events
You can emit event to:
- all clients (including the event sender)
- all clients (excluding the event sender - broadcasting. Only javascript currently supports broadcasting. PHP broadcasting coming soon)
- clients in concrete room
Global events:
Room event:
Shared Public Data
You can set shared data only from PHP using PublicData Frame (see below into PHP section).
To access data you can use getPublicData(string key, callback fn)
method
PHP
Behaviors
- YiiNodeSocket\Behaviors\ArChannel - can be used for create new channel. Example: You can attach this behavior to User, in result any user will have own channel, and other user can subscribe to events of concrete user.
- YiiNodeSocket\Behaviors\ArSubscriber - should be attached to object which can subscribe to some channel. Example: model User at a time can be channel and subscriber.
Client authorization
For authorizing client you need send special Authentication frame, you can do it in your user component in afterLogin event
After that, this user can receive events only for him. See example below, send event only for single (concrete) $user
and your javascript
Client scripts registration
Event frame
Set up shared data
You can set expiration using setLifeTime(integer $lifetime) method of class PublicData
Room events
Only member of testRoom can catch this event
Invoke client function or method
In your PHP application you can invoke javascript function or method of object in window context.
Extends from Event frame => you can send it into specific room
DOM manipulations with jquery
Task: you need update price on client side after price update in each product
Send more than one frame per a time
Example 1:
Example 2:
PS
Sorry for my english :)