Events

Ratchet is build on events to decouple everything.

Bootstrap

During initial service bootstrap the Rachet.WampServer.construct event is emitted. Only passing the event loop, it's meant for listeners (from other plugins) providing features and functionality for and through the websocket service.

RPC

RPCstands for Remote Procedure Call and allows you to call API's on the otherside of the line.

Pre-emit

Before emitting the actual event Ratchet first emits Rachet.WampServer.Rpc. Any listener an hook into it and stop it. Stopping is done as stated in the CakePHP Cookbook event section:

public function listeningMethod(CakeEvent $event) {
    $event->stopPropagation();
    // OR 
    return false;
}

In case the event is stopped actual RPC event won't be emitted. This can be used to prevent certain connections to specific calls or to throttle the amount of calls. When this happens another event is emitted: Rachet.WampServer.RpcBlocked.

Emit

Listening for the actual RPC event is simple and requires you to listen to Rachet.WampServer.Rpc.YOURRPCNAME where YOURRPCNAME is the desired RPC name that can be called from the client. For example:

class PlusOnelListener implements CakeEventListener {
    public function implementedEvents() {
        return [
            'Rachet.WampServer.Rpc.plusOne' => 'plusOne'
        ];
    }
}

The plusOne function has to call callResult sending the RPC result back to the client.

	public function plusOne($event) {
		$event->data['promise']->resolve(++$event->data['value']);
	}

Post-emit

Once the event listener has done it's job it can either resolve or reject the given promise. Wether the promise is resolved or rejected it starts sending the result back to the client before calling the Rachet.WampServer.RpcSuccess or Rachet.WampServer.RpcFailed events.

Pub/Sub

PubSub utilizes the concept of channels to let everyone in a channel know of a message. PubSub events work slightly differently and have more then 1 event to listen on.