Best Practice for Multiple Subscribe Methods in React / Ratchet / ZMQ - php

I try to build a little realtime websocket use-case, where users can login and see all other users logged in, get notified when a new user signs in or an existing user logs out.
For this scenario i use the ZMQ PUSH Socket in my UserController when a user logs in or logs out.
UserConstroller
public function login() {
//... here is the auth code, model call etc...
$aUserData = array();// user data comes from the database with username, logintime, etc....
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGIN_PUSHER'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
public function logout() {
//... here is the logout code, model call etc ....
$aUserData = array();// user data comes from the SESSION with username, logintime, etc....
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGOUT_PUSHER'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
Then i've got a Pusher class like in the Ratchet docs: link
In this class there are two methods: onUserLogin and onUserLogout and of course all the other stuff like
onSubscribe, onOpen, onPublish
UserInformationPusher
public function onUserLogin($aUserData) {
//var_dump("onUserLogin");
$sUserData = json_decode($aUserData, true);
$oTopic = $this->subscribedTopics["user_login"];
if($oTopic instanceof Topic) {
$oTopic->broadcast($sUserData);
} else {
return;
}
}
public function onUserLogout($aUserData) {
//var_dump("onUserLogout");
$entryData = json_decode($aUserData, true);
$oTopic = $this->subscribedTopics["user_logout"];
if($oTopic instanceof Topic) {
$oTopic->broadcast($entryData);
} else {
return;
}
}
The last piece is the WampServer/WsServer/HttpServer with a Loop that listens to the incoming connections. There is also my ZMQ PULL socket
RatchetServerConsole
public function start_server() {
$oPusher = new UserInformationPusher();
$oLoop = \React\EventLoop\Factory::create();
$oZMQContext = new \React\ZMQ\Context($oLoop);
$oPullSocket = $oZMQContext->getSocket(\ZMQ::SOCKET_PULL);
$oPullSocket->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$oPullSocket->on('message', array($oPusher, 'onUserLogin'));
$oPullSocket->on('message', array($oPusher, 'onUserLogout'));
$oMemcache = new \Memcache();
$oMemcache->connect('127.0.0.1', 11211);
$oMemcacheHandler = new Handler\MemcacheSessionHandler($oMemcache);
$oSession = new SessionProvider(
new \Ratchet\Wamp\WampServer(
$oPusher
),
$oMemcacheHandler
);
//$this->Output->info("Server start initiation with memcache!...");
$webSock = new \React\Socket\Server($oLoop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$oServer = new \Ratchet\Server\IoServer(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
$oSession
)
),
$webSock
);
$this->Output->info("Server started ");
$oLoop->run();
}
In this example, the call from login() or logout() would always call both methods(onUserLogin and onUserLogout).
I was not able to find some docs, which describe what events i can use in the on($event, callable $listener) method, does anyone have a link/knowledge base?
What is the best approach to check which method from the UserController was fired?
I could add some information to the $sUserData in the Controller and check this in the Pusher
I could bind an other socket to a different port (e.g. 5554 for PULL and PUSH) and use the on() method on this one
I could... is there another best practice to solve this?
No Client code needed cause it works fine

In your RatchetServerConsole,
Remove,
$oPullSocket->on('message', array($oPusher, 'onUserLogin'));
$oPullSocket->on('message', array($oPusher, 'onUserLogout'));
Add,
$oPullSocket->on('message', array($oPusher, 'onUserActionBroadcast'));
.
In your UserInformationPusher,
Remove onUserLogin() and onUserLogout().
Add,
public function onUserActionBroadcast($aUserData)
{
$entryData = json_decode($aUserData, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['topic'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['topic']];
unset($entryData['topic']);
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
.
Your UserConstroller (add the topic in $aUserData),
public function login() {
//... here is the auth code, model call etc...
$aUserData = array();// user data comes from the database with username, logintime, etc....
$aUserData['topic'] = 'USER_LOGIN'; // add the topic name
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
public function logout() {
//... here is the logout code, model call etc ....
$aUserData = array();// user data comes from the SESSION with username, logintime, etc....
$aUserData['topic'] = 'USER_LOGOUT'; // add the topic name
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
.
Finally in your view file,
<script>
var conn = new ab.Session('ws://yourdomain.dev:9000', // Add the correct domain and port here
function() {
conn.subscribe('USER_LOGIN', function(topic, data) {
console.log(topic);
console.log(data);
});
conn.subscribe('USER_LOGOUT', function(topic, data) {
console.log(topic);
console.log(data);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
.
NOTE: The basic idea was to use a single broadcast function in the pusher class.

After one month of intensive handling with PHPs best practice in websockets i changed from my approach to the Crossbar.io, voryx/Thruway in the PHP Backend and Autobahn|JS in the Frontend.
All of these componentes support the WAMP V2 Websocket Standard and are able to handle my requirements.
If there are some requests i can post the solution to my problem above, with the usage of the mentioned components.

Related

Send event from php controller

Good morning everyone.
I need to send a notification whenever a user swipes a tab on a particular sensor.
The problem is not the connection to the sensor, which at this moment already takes place and subject to user access.
Currently I have created a server socket inside my yii2 app to be able to send the notification event to the client and update them in real time.
This is my controller server
class ServerController extends Controller
{
public function actionStart()
{
// $server = new CommandsServer();
$server = new ChatServer();
$server->port = 80; //This port must be busy by WebServer and we handle an error
$server->on(WebSocketServer::EVENT_WEBSOCKET_OPEN_ERROR, function ($e) use ($server) {
echo "Error opening port " . $server->port . "\n";
$server->port += 1; //Try next port to open
$server->start();
});
$server->on(WebSocketServer::EVENT_WEBSOCKET_OPEN, function ($e) use ($server) {
echo "Server started at port " . $server->port;
});
$server->start();
}
}
This is my chat server which I created for testing
<?php
namespace frontend\daemons;
use consik\yii2websocket\events\WSClientEvent;
use consik\yii2websocket\WebSocketServer;
use Ratchet\ConnectionInterface;
class ChatServer extends WebSocketServer
{
public function init()
{
parent::init();
$this->on(self::EVENT_CLIENT_CONNECTED, function (WSClientEvent $e) {
$e->client->name = null;
});
}
protected function getCommand(ConnectionInterface $from, $msg)
{
$request = json_decode($msg, true);
return !empty($request['action']) ? $request['action'] : parent::getCommand($from, $msg);
}
public function commandPing(ConnectionInterface $client, $msg)
{
$arr = ["Neo", "Morpheus", "Trinity", "Cypher", "Tank"];
$res = ['type' => 'ping', 'message' => json_encode(array_rand($arr, 1))];
foreach ($this->clients as $chatClient) {
$chatClient->send(json_encode($res));
}
}
}
Now, what I wish I could do is be able to use that commandPing inside another controller, but I haven't found a way I can implement this.
In this sense the round would be:
user swipes card on sensor -> the sensor calls my method to see if the user is actually authorized to enter -> I call commandPing (as an example) to send a notification to the customer (OK / KO )
On the web interface side I will then intercept the message via new Websocket (but this is not a problem)

Ratchet Websocket async mysql best practice

I've build a Websocket chat based on ratchet that uses reactphp async mysql and just got a couple of questions to make sure I'm doing things right, as I couldn't really find any examples for that case out there.
The use case of the Socket is a livechat that handles many different pages at the same time, about 20 pages with 1000+ Users.
Actually this works for now with 100 users, but I have doubts about the database connection to block or queue too many queries with more than thousand users at the same time.
I need to make sure the server can handle everything fast.
Ratchet: https://github.com/ratchetphp/Ratchet
Mysql: https://github.com/friends-of-reactphp/mysql
So, the Socket will be used by different applications, each of them has it's own database where the data has to be stored and published, which means there are multiple Lazy connections created, this is done by the Ratchet Pusher.
There is one database locally on the Websocket server that stores all connected interfaces in one table, out of there the connections are distributed.
The push-server starts the event loop and passes the mysql factory to the pusher constructor (push-server.php):
require dirname(__DIR__).'/websocket/vendor/autoload.php';
use React\MySQL\Factory;
use React\MySQL\QueryResult;
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$pusher = new websocket01\Pusher($factory);
$webSock = new React\Socket\Server('0.0.0.0:8090', $loop);
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
The Pusher then creates a lazy connection for each connected Interface in the constructor, and thats my second question: Is it better to create just one async db connection at all and keep it alive (like now), or is is better to reopen the connection at every client interaction ?
(Pusher.php):
protected $connection;
protected $subconnections = array();
public function __construct($factory){
//Main Websocket db
$uri = 'localhost...';
$this->connection = $factory->createLazyConnection($uri);
//Create array with Connections to Sub dbs for every entry in the interface table
$stream = $this->connection->queryStream('SELECT * from interfaces');
$stream->on('data', function ($interface) use ($factory) {
if (!array_key_exists($interface['interface_dbhost'], $this->subconnections)) {
$uri = "".$interface['interface_dbuser'].":".$interface['interface_dbpass']."#".$interface['interface_dbhost'].":".$interface['interface_dbport']."/".$interface['interface_dbname']."";
$this->subconnections[$interface['interface_dbhost']] = $factory->createLazyConnection($uri);
}
});
$stream->on('end', function () {
echo 'Completed.';
});
}
The main (local) database will still be used to save clients data, errors, verify tokens etc.
The right database connection will then be chosen in onopen, onsubscribe, onpublish... automatically out of the subconnections array, depending on the application the client comes from, and the action he wants to perfom, for e.g:
public function onPublish(ConnectionInterface $conn, $conversation_id, $event, array $exclude, array $eligible) {
$obj = json_decode($event);
$interface = $obj->frontend;
$channel = $obj->channel;
$db = $this->selectDatabase($conversation_id, $interface); //Choose right database connection
$clienthandler = new Clienthandler\Clienthandler($this->connection);
$action = $this->processRequest($db,$obj, $conversation_id, $clienthandler, $operatorhandler, $channel);
}
I'm also passing the local db connection to the clienthandler too, as I need it there to log actions.
public function selectDatabase($conversation_id, $interface) {
if(array_key_exists($interface, $this->subconnections)) {
$dbconn = $this->subconnections[$interface];
$dbconn->ping()->then(function () {
echo 'Connection alive' . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}
}
return $dbconn;
}
The clienthandler then saves the data and broadcasts it to all subscribers
I'm open for any advice :-)
Thank you for your help

How to run a computation while running a websocket server in PHP?

I have the following scenario:
I have an API built with the Slim PHP framework. I am using the PHP lib Ratchet to run a WebSocket server. Once the WebSocket server is started, I want to run a function that does some computation while the server is running.
So far, inside my API, I have a route that calls the MyMethod method of a class MyClass. Inside the class, I have the following:
class MyClass {
public $calculation_status;
public function MyMethod() {
$server = IoServer::factory(
new HttpServer(
new WsServer(
new messengerApp($this)
)
),
8080
);
$this->doCalculationAsynchronously()->then(
function ($result) {
$this->calculation_status = 'finished';
},
function ($reason) {
$this->calculation_status = 'stopped';
},
function ($update) {
$this->calculation_status = 'still working...';
}
}
$server->run($this);
}
public function doCalculationAsynchronously() {
$deferred = new Deferred();
$this->computeSomethingAsync(function ($error = null, $result) use ($deferred) {
if ($error) {
$deferred->reject($error);
} else {
$deferred->resolve($result);
}
});
return $deferred->promise();
}
public function computeSomethingAsync() {
// Simulate a long running calculation
while(true){} // OR sleep(1000000);
return $result;
}
}
So, I'm expecting this to try to start running the asynchronous calculation function, return a promise to MyMethod, and then run my WebSocket server.
The reason for injecting $this into the server is to access my calculation_status property and be able to send it to clients through the WS.
This code is inspired by the example for Deferred in the ReactPHP doc
Note: If I don't have the forever while loop, it goes on and runs the server correctly (but this is synchronous behavior; my goal for the server is to send the calculation status to clients). Injecting the class into the object works fine as well.

How to inject more complex service in slim3 such as Paytrail

Below is an example code for "Paytrail_Module_Rest.php", a set of classes for interacting with a rest api for a payment gateway. Some of the classes can be instantiated ahead of time such as (Paytrail_Module_rest which holds credentials), but some need to be instantiated with information only available in the controller, (such as Paytrail_Module_Rest_Payment_S1 which sets payment details such as price)
Can anyone suggest a clean way of injecting it into slim3? I can't see any good way of doing it with the standard container injection methods.
$urlset = new\App\Service\Paytrail\Paytrail_Module_Rest_Urlset(
"https://www.demoshop.com/sv/success", // return address for successful payment
"https://www.demoshop.com/sv/failure", // return address for failed payment
"https://www.demoshop.com/sv/notify", // address for payment confirmation from Paytrail server
"" // pending url not in use
);
$orderNumber = '1';
$price = 99.00;
$payment = new \App\Service\Paytrail\Paytrail_Module_Rest_Payment_S1($orderNumber, $urlset, $price);
$payment->setLocale('en_US');
$module = new \App\Service\Paytrail\Paytrail_Module_Rest(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ');
try {
$result = $module->processPayment($payment);
}
catch (\App\Service\Paytrail\Paytrail_Exception $e) {
die('Error in creating payment to Paytrail service:'. $e->getMessage());
}
echo $result->getUrl();
( credentials listed here are public test credentials )
Add the stuff that doesn't change to the container like the module and the urlset thingy
$container[\App\Service\Paytrail\Paytrail_Module_Rest_Urlset::class] = function($c) {
return new \App\Service\Paytrail\Paytrail_Module_Rest_Urlset(
"https://www.demoshop.com/sv/success", // return address for successful payment
"https://www.demoshop.com/sv/failure", // return address for failed payment
"https://www.demoshop.com/sv/notify", // address for payment confirmation from Paytrail server
"" // pending url not in use
);
};
$container[\App\Service\Paytrail\Paytrail_Module_Rest::class] = function($c) {
return new \App\Service\Paytrail\Paytrail_Module_Rest(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ');
};
And then you either can instantiate the payment every time you need or add a helper class like an adapter:
class PaymentAdapter {
public function __construct(
\App\Service\Paytrail\Paytrail_Module_Rest $module,
\App\Service\Paytrail\Paytrail_Module_Rest_Urlset $urlset)
{
$this->module = $module;
$this->urlset = $urlset;
}
function createAndProcessPayment($orderNumber, $price)
{
$payment = new \App\Service\Paytrail\Paytrail_Module_Rest_Payment_S1($orderNumber, $this->urlset, $price);
$payment->setLocale('en_US');
try {
$result = $module->processPayment($payment);
}
catch (\App\Service\Paytrail\Paytrail_Exception $e) {
die('Error in creating payment to Paytrail service:'. $e->getMessage());
}
return $result;
}
}
Then add the adapter also to the container:
$container[\yournamespace\PaymentAdapter::class] = function($c) {
return new \yournamespace\PaymentAdapter(
$c[\App\Service\Paytrail\Paytrail_Module_Rest::class],
$c[\App\Service\Paytrail\Paytrail_Module_Rest_Urlset::class]
);
};

AS2: load class variables with sendandload

I'm using Actionscript 2.0 in combination with PHP, now I can make a call to my PHP file and receive data but apparently I have to use that data immediately, I cannot use it to fill my class variables.
This is what I want :
class user {
var lastname:String;
function user(in_ID:Number){
var ontvang:LoadVars = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
ontvang.onLoad = function(success:Boolean) {
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
};
}
}
I've found out that this is a big issue in AS2, I found this post to work around it if you're loading XML data but I can't seem to get it to work with LoadVars :
http://www.actionscript.org/forums/showthread.php3?t=144046
Any help would be appreciated ..
When your onLoad handler is called, it is being called as if it were a member function of the LoadVars instance, and not your user instance.
There are several ways around this, one is to use Delegate.create() to create a function which will work as intended, for example:
import mx.utils.Delegate;
class user {
var lastname:String;
var ontvang:LoadVars;
function user(in_ID:Number){
ontvang = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
ontvang.onLoad = Delegate.create(this, onLoad);
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
};
}
function onLoad(success:Boolean) : Void
{
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
}
}
Don't forget that the load is asynchronous - when you create one of your user objects, the member variables won't be immediately available. What you may need to do is let your user object be capable of signaling its readiness much like LoadVars does, (e.g. with a callback function provided by the caller) so that your app is driven by by these asynchronous events.

Categories