php - Route requests to Laravel Controller to running WebSocket server -


the situation:

there long-running task needs launched asynchronously. details of task aren't important (the basics multiple vms going provisioned , complex network setup), , being handled python script running on different server. decided go websockets communication , forth between web server , client, , have bi-directional communication there working. web server sending requests other server, , receive http post notifications python script when each machine , running, , final http post when entire network up.

all of works. framework we're using on web server laravel 4, , web socket server built using ratchet, , implemented in artisan command. issue i'm not sure how relay http posts laravel controller python script websocket server in turn can relay information client.

below implementation of ratchet's messagecomponentinterface::onmessage method, using observer pattern notify subscribed listeners of message event.

// class: socketmessenger public function onmessage(connectioninterface $from, $msg) {     $respondto = null;     foreach($this->_clients $client) {         if($client->resourceid == $from->resourceid) {             $respondto = $client;             break;         }     }      $msg = json_decode($msg, true);      if(!$respondto || !$msg || !isset($msg['type'])) {         echo 'malformed socket message received, rejecting.';         return;     }      $type = $msg['type'];      // notify subscribed listeners message type of      // incoming message. response received relayed      // client.     if(isset($this->_listeners[$type])) {         foreach( $this->_listeners[$type] $listener ) {             $response = $listener->notify( $respondto, $msg );             if($response) {                 $respondto->send( $response );             }         }     } } 

the websocket server constructed within service provider this:

// class: socketserviceprovider $app->bind( 'ratchet\messagecomponentinterface', function( $app, $params ) {     $port = $params && isset($params[0]) ?         $params[0] : self::default_port     ;      $messenger = new socketmessenger;      // setup ratchet server.     $server = ioserver::factory(         new httpserver(             new wsserver(                  $messenger             )         ),         $port     );      // subscribe socket listeners socket messenger     $messenger->subscribe( new topologymessagelistener, topologymessagelistener::type );      return $server; } ); 

is there way take requests laravel controller , notify running websocket server whenever given controller's method hit?

the answer here appears need create websocket client in php send messages websocket server, same client written in javascript would. interested viewers struggling same issue, able find few websocket clients written in php:

  1. https://github.com/gabrielbull/php-websocket-client
  2. https://github.com/devristo/phpws
  3. https://github.com/symbiose/php-websocket-client (<-- 1 wound going with)

i'll leaving question open awhile in case comes different idea / better client library use.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -