Creating a Custom PHP WAMP Client for Thruway

There are a few different options you have when creating a WAMP (Web Application Messaging Protocol) client with Thruway.

Option 1 is to use the Connection object provided with Thruway. This object was meant to ease the transition for those people familiar with simple use of the AutobahnJS Connection object. There is an example of this in the sources here.

Option 2 is to use the Thruway Client object as is – hooking into it with event handlers. There is an example of this in the sources here.

Option 3 is to subclass the Client object. That is what I am going to cover in this post.

Which option you chose would probably be a stylistic choice, or maybe what you are most comfortable with.

For this example, I will walk you through building a client that will allow querying free filesystem space via WAMP.

To get started, follow the instructions in the readme. There is no need to start the server as it instructs at the end of the installation. This example will use the thruway demo server so you don’t have to worry about that end.

You should be in the thruway directory now.

We are going to create two files: FreeSpaceClient.php, which will contain our subclass of Client, and StartClient.php, which will be used to start the Client.

First the FreeSpaceClient.

Create the FreeSpaceClient.php file and then copy and paste this into it:

require __DIR__ . '/vendor/autoload.php';

class FreeSpaceClient extends Thruway\Peer\Client {

This just requires the autoloader so PHP can find our classes and sets up the new class for us to work in.

Make the method that is going to be our RPC call:

public function getFreeSpace() {
    return array(disk_free_space('/')); // use c: for you windowers

Note that all RPCs must return arrays. They can be arrays of object but it must be an array.

We wire up our RPC in the onSessionStart by overriding it. Add this method to your class:

public function onSessionStart($session, $transport) {

Inside this method we will register our RPC call by adding:

$this->getCallee()->register($session, 'com.example.getfreespace', array($this, 'getFreeSpace'));

This is it for our client object.

Create a new file StartClient.php that will bootstrap our client:

require __DIR__ . '/vendor/autoload.php'; // so PHP can find Thruway etc.
require __DIR__ . '/FreeSpaceClient.php'; // so PHP can use our class

$client = new FreeSpaceClient('myrealm');

$client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider('ws://'));


Now you can run the client and it should connect to the demo server and join the myrealm realm (you may want to change this if you run into other people trying this example).

I have created a plunker for you to test your creation here.

7 responses to “Creating a Custom PHP WAMP Client for Thruway

  1. The example above works fine with the example server (SimpleWsServer.php) but if I try to use (0.9.9) the PHP RPC handler keeps disconnecting and reconnecting, and I don’t get the result back to the caller.

    Is it meant to be compatible? I see the same occur for any RPC with Thruway / Any suggestions? Thanks

  2. Hi Steve,

    This example should work fine with crossbar. To make sure, I installed the crossbar php example (which uses Thruway) and replaced the client in there with this one.

    I did have to make a few changes: the URL I had to change to “ws://” and the realm I had to change to “realm1”. I made this change in both plunkr and the php client and was able to see the disk space free on my system.

    The constant reconnecting is likely due to this as the default client will try to reconnect whenever a connection is lost.

    Let me know if this helps or if you still can’t get it going.


    1. Hi

      Thanks for your prompt reply. I tried the crossbar PHP example again (entirely untouched) and it does the same – i.e. the guest process closes its connection and so takes down the crossbar broker as soon as I open the web page (localhost:8080).

      I’ve not modified crossbar or the PHP example at all… I’ll keep digging to see if I can see what’s up.


        1. Hi Dave

          Sorry it’s been a while – I’ve been tied up on other things- but I tried your example and it works OK now. That said, the original example now also works. (my updated crossbar installation now says 0.9.11 so something must have been fixed in there.)


          FYI – OS is Xubuntu (3.13.0-39-generic) and PHP is 5.5.9-1ubuntu4.5

  3. What is the recommended way to close a client connection? I have a Laravel controller that opens a client connection to the WS server, publishes a message to a channel and should then disconnect.

    I assume I should close the client right away since the Laravel session doesn’t remain active. I’m currently trying $session->close(), but I’d like to know if that’s the “proper” way.

Leave a Reply