PHP code example of rsocket / rsocket-php

1. Go to this page and download the library: Download rsocket/rsocket-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rsocket / rsocket-php example snippets


Scheduler::setDefaultFactory(function () {
    return new Scheduler\EventLoopScheduler(Loop::get());
});

$rsocketPromise = RSocketConnector::create()->connect("tcp://127.0.0.1:42252");

$rsocketPromise->then(function (RSocket $rsocket) {
    $observablePayload = $rsocket->requestResponse(Payload::fromText("text/plain", "Ping"));
    $observablePayload->subscribe(
        function (Payload $x) {
            echo 'Result: ' . $x->getDataUtf8();
        }
    );
});

Scheduler::setDefaultFactory(function () {
    return new Scheduler\EventLoopScheduler(Loop::get());
});

$listenUri = "tcp://127.0.0.1:42252";
$socketAcceptor = CallableSocketAcceptor::handle(function ($setupPayload, $sendingRSocket) {
    return AbstractRSocket::requestResponseHandler(function ($payload) {
        return Observable::of(Payload::fromText("metadata", "PONG"));
    });
});
$server = RSocketServer::create($socketAcceptor)->bind($listenUri);
print("RSocket Server started on ${listenUri}");