Sending messages using php with voryx Thruway WAMP messaging system

I am trying to create a notification message system. Im using SimpleWsServer.php server example . I want to send a notification to the user's browser when a task is completed on the server. This needs to be done with PHP and I cannot find a tutorial showing this. All the tutorials seem to show the tavendo / AutobahnJS scripts to send and receive while the PHP server is running as a manager.

Php script

+3


source to share


1 answer


Astro,

This is actually pretty straight forward and can be done in several different ways. We developed the Thruway client to mimic the AutobahnJS client, so most of the simple examples will be streamed live.

I assume you want to post it from a website (not long php script).

In your PHP website, you will 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 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) would look like:

var connection = new autobahn.Connection({
    url: 'ws://demo.thruway.ws:9090',  //You can use this demo server or replace it with your router 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 also created a plunker for the javascript side and a runnable for the PHP side.

+7


source







All Articles