Skip to content
Advertisement

Passing variable to php websocket

I cant figure out, how to pass variables to websocket. For example, I get calculations before launching websocket, and I would love to pass results to websocket, to compare them on each update until I get result I’m waiting for. Is there a way to do it? I was able to kind if make it work by creating cache, and reading it on message update, but takes way too much resources, and memory gets clogged very fast.

$variable = 151481;

RatchetClientconnect('wss://fstream.binance.com/stream?streams='.$pairs)->then(function($conn) {
    $conn->on('message', function($msg) use ($conn) {
        $result = json_decode($msg);

        $open = $result->data->k->o;
        $close = $result->data->k->c;

        //REQUIRED VARIABLE 
        //$variable = ??;

    });
}, function ($e) {
    echo "Could not connect: {$e->getMessage()}n";
 });

Advertisement

Answer

I was able to pass variables with $GLOBALS['variable']; With these I can pass values from one websocket message to another and memory resources usage is fairly low.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement