1. Go to this page and download the library: Download phuongdev89/yii2-socketio 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/ */
use phuongdev89\socketio\events\EventInterface;
use phuongdev89\socketio\events\EventPubInterface;
class CountEvent implements EventInterface, EventPubInterface
{
/**
* Channel name. For client side this is nsp.
*/
public static function broadcastOn(): array
{
return ['notifications'];
}
/**
* Event name
*/
public static function name(): string
{
return 'update_notification_count';
}
/**
* Emit client event
* @param array $data
* @return array
*/
public function fire(array $data): array
{
return $data;
}
}
//Run broadcast to client
\phuongdev89\socketio\Broadcast::emit(CountEvent::name(), ['count' => 10]);
use phuongdev89\socketio\events\EventInterface;
use phuongdev89\socketio\events\EventSubInterface;
class MarkAsReadEvent implements EventInterface, EventSubInterface
{
/**
* Changel name. For client side this is nsp.
*/
public static function broadcastOn(): array
{
return ['notifications'];
}
/**
* Event name
*/
public static function name(): string
{
return 'mark_as_read_notification';
}
/**
* Emit client event
* @param array $data
* @return array
*/
public function handle(array $data)
{
// Mark notification as read
// And call client update
file_put_contents(\Yii::getAlias('@app/file.txt'), json_encode($data));
}
}
use phuongdev89\socketio\events\EventSubInterface;
use phuongdev89\socketio\events\EventInterface;
use phuongdev89\socketio\events\EventPolicyInterface;
class MarkAsReadEvent implements EventInterface, EventSubInterface, EventPolicyInterface
{
/**
* Changel name. For client side this is nsp.
*/
public static function broadcastOn(): array
{
return ['notifications'];
}
/**
* Event name
*/
public static function name(): string
{
return 'mark_as_read_notification';
}
/**
* @param $data
* @return bool
*/
public function can($data): bool
{
// Check data from client
return true;
}
/**
* Emit client event
* @param array $data
* @return array
*/
public function handle(array $data)
{
// Mark notification as read
// And call client update
file_put_contents(\Yii::getAlias('@app/file.txt'), json_encode($data));
}
}
use phuongdev89\socketio\events\EventPubInterface;
use phuongdev89\socketio\events\EventInterface;
use phuongdev89\socketio\events\EventRoomInterface;
class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface
{
/**
* User id
* @var int
*/
protected $user_id;
/**
* Channel name. For client side this is nsp.
*/
public static function broadcastOn(): array
{
return ['notifications'];
}
/**
* Event name
*/
public static function name(): string
{
return 'update_notification_count';
}
/**
* Socket.io room
* @return string
*/
public function room(): string
{
return 'user_id_' . $this->user_id;
}
/**
* Emit client event
* @param array $data
* @return array
*/
public function fire(array $data): array
{
$this->user_id = $data['user_id'];
return [
'count' => 10,
];
}
}
use phuongdev89\socketio\events\EventPubInterface;
use phuongdev89\socketio\events\EventInterface;
use phuongdev89\socketio\events\EventRoomInterface;
use phuongdev89\socketio\traits\ListenTrait;
class CountEvent implements EventInterface, EventPubInterface, EventRoomInterface
{
use ListenTrait;
/**
* User id
* @var int
*/
protected $userId;
/**
* Channel name. For client side this is nsp.
*/
public static function broadcastOn(): array
{
return ['notifications'];
}
/**
* Socket.io room
* @return string
*/
public function room(): string
{
return 'user_id_' . $this->userId;
}
/**
* Emit client event
* @param array $data
* @return array
*/
public function fire(array $data): array
{
$this->userId = $data['userId'];
return [
'count' => 10,
];
}
public function handle(array $data)
{
$this->listen($data); //must place before your code
file_put_contents(\Yii::getAlias('@app/../file.txt'), serialize($data));
}
public function onLeave($room_id)
{
// TODO: Implement onLeave() method.
}
public function onDisconnect($room_id)
{
// TODO: Implement onDisconnect() method.
}
public function onJoin($room_id)
{
// TODO: Implement onJoin() method.
}
}
//Run broadcast to user id = 10
\phuongdev89\socketio\Broadcast::emitToRoom(CountEvent::class, [
'count' => 4,
'user_id' => 10,//push data to room-10
]);
bash
php yii socketio/start
bash
php yii socketio/stop
js
var socket = io('localhost:1367/notifications');
socket.emit('mark_as_read_notification', {id: 10});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.