The WAMP (WebSocket Application Messaging Protocol) specification gives developers (that's you!) a more structured and easy way for your client (JavaScript) and server (Ratchet-PHP) to interact.WAMP provides the RPC and PubSub patterns.WAMP specifies URIs for endpoint handles and JSON for payload transmissions. It is recommended taking a quick read over the WAMP Specification to get an understanding of how and why to use it.
If you choose to build your application on theWAMP spec (highly recommended) you will need a JavaScript library to implement the client side. AutobahnJS is a client to interact withWAMP servers and is highly recommended to use with Ratchet. Using AutobahnJS has a requirement of a deferred library. They recommend using when or jQuery's deferred library. The programming reference for AutobahnJS can be found on the Autobahn web site.
Note: Although the documentation specified to use URI's as Topic context, there is no enforcement on client or server; you can use any string to identify these topics.
The WampServer component provides a new class that is passed to your application: the Topic class. This is a container that stores all of the Connections who have subscribed to that topic. It also features some useful methods such as broadcast to send a message to every one of its subscribers.
When calling Connection methods that require "string $topic" you can pass the Topic class or its id.
If you are familiar with other forms of messaging, topics are equivalent to channels.
As found in the API Docs: Triggered events are propagated through a WampServerInterface object passed to the __construct.
None
<?php // Your shell script $server = new \Ratchet\App('localhost'); $server->route('/pubsub', new BasicPubSub); $server->run();
<?php use Ratchet\ConnectionInterface as Conn; /** * When a user publishes to a topic all clients who have subscribed * to that topic will receive the message/event from the publisher */ class BasicPubSub implements Ratchet\Wamp\WampServerInterface { public function onPublish(Conn $conn, $topic, $event, array $exclude, array $eligible) { $topic->broadcast($event); } public function onCall(Conn $conn, $id, $topic, array $params) { $conn->callError($id, $topic, 'RPC not supported on this demo'); } // No need to anything, since WampServer adds and removes subscribers to Topics automatically public function onSubscribe(Conn $conn, $topic) {} public function onUnSubscribe(Conn $conn, $topic) {} public function onOpen(Conn $conn) {} public function onClose(Conn $conn) {} public function onError(Conn $conn, \Exception $e) {} }