'websocket and file_get_contents

I have a remote server that regularly posts data to my web server. I would like to consume the data posted by this server in my PHP websocket server. I use ratchet to implement the websocket server. how to consume the data posted by my remote server in my websocket knowing that the remote server is not connected to the websocket. it only posts the data. I would like to continue to use the data contained in the variable "$data" in my class "SocketServer" once instantiated

ob_start();
header("HTTP/1.1 200 OK");
header("Content-length: " . (string)ob_get_length());
ob_end_flush();

$data = json_decode(file_get_contents("php://input"), true);


require '../../tools/ratchet/vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

define('SOCKET_PORT',10000);



class SocketServer implements MessageComponentInterface{

    protected $clients;
    private $subscriptions;
    private $users;


    public function __construct(){
        $this->clients = new SplObjectStorage;
        $this->subscriptions = [];
        $this->users = [];
    }
    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
        $this->user[$conn->resourceId] = $conn;
        echo "New connection! ({$conn->resourceId}).\n";  
    }
    public function onMessage(ConnectionInterface $conn, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('new Connection! %d sending message "%s" to %d other connection%s' . "\n\n\n"
            , $conn->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        $msg = json_decode($msg);
        switch($msg->command){
            case "subscribe":
                $this->subscriptions[$conn->resourceId] = $msg->message->id_user;
                echo sprintf("user id '%d' : \n\n\n",$this->subscriptions[$conn->resourceId]);
                break;
                default:

                    break;
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
        unset($this->users[$conn->resourceId]);
        unset($this->subscriptions[$conn->resourceId]);
        echo "Connection {$conn->resourceId} is gone.\n";
   }

   public function onError(ConnectionInterface $conn, \Exception $e) {
    echo "An error occured on connection {$conn->resourceId}: {$e->getMessage()}\n\n\n";
    $conn->close();
   }


}

$wsServer = new WsServer(new SocketServer()) ;
$server = IoServer::factory(new HttpServer($wsServer),SOCKET_PORT);
$wsServer->enableKeepAlive($server->loop, 30);
echo "Server created...... " ."\n\n";
echo "Server created on port " . SOCKET_PORT ."\n\n";
$server->run();
?>```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source