wyrihaximus/ratchet-commands

Brings 2 way communication to wyrihaximus/ratchet.

Installs: 61 526

Dependents: 1

Suggesters: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 5

Type:cakephp-plugin

0.1.0 2015-01-22 16:05 UTC

README

Build Status Latest Stable Version Total Downloads Coverage Status Bitdeli Badge

RatchetCommands lets your application communicate messages to the Ratchet server instance.

Getting started

1. Requirements

This plugin depends on the following plugin and libraries and are pulled in by composer later on:

2. Composer

Make sure you have composer installed and configured with the autoloader registering during bootstrap as described here. Make sure you have a composer.json and add the following to your required section.

composer require wyrihaximus/ratchet-commands 

3. Setup the plugin

Make sure you load RatchetCommands in your bootstrap and the Ratchet plugin is setup properly.

4. Building a command

Filename: Lib/MessageQueue/Command/BroadcastCommand.php

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
		}
	}
}

Besure to App::uses the command in your bootstrap so the Ratchet instance knows where to load it from.

5. Sending the message

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

Plugin License

(The MIT License)

Copyright © 2012 - 2013 Cees-Jan Kiewiet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.