3

I'm trying to build a notification messaging system. Im using the SimpleWsServer.php server example. I want to push a notification to the user's browser when a task has completed on the server. This needs to be done using PHP and i cant find a tutorial where this is shown. All the tutorials seems to be showing tavendo/AutobahnJS scripts to send and receive while the PHP server runs as a manager.

Is it possible to send a message using a php script to the subscribers?

Cœur
  • 37,241
  • 25
  • 195
  • 267
astroanu
  • 3,901
  • 2
  • 36
  • 50

1 Answers1

7

Astro,

This is actually pretty straight forward and can be accomplished a couple of different ways. We designed the Thruway Client to mimic the AutobahnJS client, so most of the simple examples will translate directly.

I'm assuming that you want to publish from a website (not a long running php script).

In your PHP website, you'll want to do something like this:

$connection = new \Thruway\Connection(
    [
        "realm"   => 'com.example.astro',
        "url"     => 'ws://demo.thruway.ws:9090', //You can use this demo server or replace it with your router's IP
    ]
);

$connection->on('open', function (\Thruway\ClientSession $session) use ($connection) {

    //publish an event
    $session->publish('com.example.hello', ['Hello, world from PHP!!!'], [], ["acknowledge" => true])->then(
        function () use ($connection) {
            $connection->close(); //You must close the connection or this will hang
            echo "Publish Acknowledged!\n";
        },
        function ($error) {
            // publish failed
            echo "Publish Error {$error}\n";
        }
    );
  });

 $connection->open();

And the javascript client (using AutobahnJS) will look like this:

var connection = new autobahn.Connection({
    url: 'ws://demo.thruway.ws:9090',  //You can use this demo server or replace it with your router's IP
    realm: 'com.example.astro'
});

connection.onopen = function (session) {

    //subscribe to a topic
    function onevent(args) {
        console.log("Someone published this to 'com.example.hello': ", args);    
    }

    session.subscribe('com.example.hello', onevent).then(
        function (subscription) {
            console.log("subscription info", subscription);
        },
        function (error) {
           console.log("subscription error", error);
        }
    );
};

connection.open();

I've also created a plunker for the javascript side and a runnable for the PHP side.

daviddan
  • 416
  • 2
  • 8
  • 1
    Oh my gosh $connection->close(); why didnt i get that before. thank you .. I love how you guys have made it to mimic autobahn. thanks again ! – astroanu Oct 02 '14 at 05:22
  • The runnable link doesn't seem to work anymore. I can run this code successfully one at a time, but I I'd like to keep the socket open and hook into it quickly from the PHP side. How does that work? – Joe Leonard Dec 23 '18 at 21:10
  • @JoeLeonard Did you figure out how to keep the connection open? I'm about to face the same challenge and any help would be much appreciated. – dearsina Apr 03 '20 at 08:04
  • I ended up installing ZeroMQ. I found server push using this method was way too slow. I can push about 1,000 messages from the server in 8 milliseconds with ZeroMQ. – Joe Leonard Apr 05 '20 at 01:45