PHP code example of wyrihaximus / ratchet-commands

1. Go to this page and download the library: Download wyrihaximus/ratchet-commands 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/ */

    

wyrihaximus / ratchet-commands example snippets


class BroadcastCommand extends RatchetMessageQueueCommand {

	public function serialize() {
		return serialize(array(
			'data' => $this->data,
		));
	}

	public function unserialize($commandString) {
		$commandString = unserialize($commandString);
		$this->setData($commandString['data']);
	}

	public function setData($data) {
		$this->data = $data;
	}

	public function getData() {
		return $this->data;
	}

	// Send a broadcast on the channel `channel` with the data sent with the command
	// (This runs in the Ratchet instance.)
	public function execute($eventSubject) {
		$topics = $eventSubject->getTopics();
		if (isset($topics['channel'])) {
			$topics['channel']->broadcast($this->getData());
		}

		return true;
	}

	// Handle the response
	// (This runs in the application.)
	public function response($response) {
		if ($response) {
			// Handle success
		} else {
			// Handle failure
		}
	}
}

App::uses('TransportProxy', 'RatchetCommands.Lib/MessageQueue/Transports');
$command = new BroadcastCommand();
$command->setData([
	'time' => time(),
]);
TransportProxy::instance()->queueMessage($command);