Coder Social home page Coder Social logo

fbns-react's Introduction

fbns-react

A PHP client for the FBNS built on top of ReactPHP.

Requirements

You need to install the GMP extension to be able to run this code on x86 PHP builds.

Installation

composer require valga/fbns-react

Basic Usage

// Set up a FBNS client.
$loop = \React\EventLoop\Factory::create();
$client = new \Fbns\Client\Lite($loop);

// Read saved credentials from a storage.
$auth = new \Fbns\Client\Auth\DeviceAuth();
try {
    $auth->read($storage->get('fbns_auth'));
} catch (\Exception $e) {
}

// Connect to a broker.
$connection = new \Fbns\Client\Connection($deviceAuth, USER_AGENT);
$client->connect(HOSTNAME, PORT, $connection);

// Bind events.
$client
    ->on('connect', function (\Fbns\Client\Lite\ConnectResponsePacket $responsePacket) use ($client, $auth, $storage) {
        // Update credentials and save them to a storage for future use.
        try {
            $auth->read($responsePacket->getAuth());
            $storage->set('fbns_auth', $responsePacket->getAuth());
        } catch (\Exception $e) {
        }
        
        // Register an application.
        $client->register(PACKAGE_NAME, APPLICATION_ID);
    })
    ->on('register', function (\Fbns\Client\Message\Register $message) use ($app) {
        // Register received token with an application.
        $app->registerPushToken($message->getToken());
    })
    ->on('push', function (\Fbns\Client\Message\Push $message) use ($app) {
        // Handle received notification payload.
        $app->handlePushNotification($message->getPayload());
    });

// Run main loop.
$loop->run();

Advanced Usage

// Set up a proxy.
$connector = new \React\Socket\Connector($loop);
$proxy = new \Clue\React\HttpProxy('username:[email protected]:3128', $connector);

// Disable SSL verification.
$ssl = new \React\Socket\SecureConnector($proxy, $loop, ['verify_peer' => false, 'verify_peer_name' => false]);

// Enable logging to stdout.
$logger = new \Monolog\Logger('fbns');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::INFO));

// Set up a client.
$client = new \Fbns\Client\Lite($loop, $ssl, $logger);

// Persistence.
$client->on('disconnect', function () {
    // Network connection has been closed. You can reestablish it if you want to.
});
$client->connect(HOSTNAME, PORT, $connection)
    ->otherwise(function () {
        // Connection attempt was unsuccessful, retry with an exponential backoff.
    });

fbns-react's People

Contributors

valga avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.