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?
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.