2017 © Pedro Peláez
 

library gripcontrol

A GRIP library for PHP.

image

fanout/gripcontrol

A GRIP library for PHP.

  • Wednesday, August 2, 2017
  • by kon
  • Repository
  • 5 Watchers
  • 12 Stars
  • 7,580 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 13 Versions
  • 13 % Grown

The README.md

php-gripcontrol

Author: Konstantin Bokarius kon@fanout.io, (*1)

A GRIP library for PHP., (*2)

License

php-gripcontrol is offered under the MIT license. See the LICENSE file., (*3)

Requirements

  • openssl
  • curl
  • pthreads (required for asynchronous publishing)
  • firebase/php-jwt >=1.0.0 (retreived automatically via Composer)
  • fanout/php-pubcontrol >=1.0.6 (retreived automatically via Composer)

Installation

Using Composer: 'composer require fanout/gripcontrol', (*4)

Manual: ensure that php-jwt and php-pubcontrol have been included and require the following files in php-gripcontrol:, (*5)

require 'php-gripcontrol/src/encoding.php';
require 'php-gripcontrol/src/channel.php';
require 'php-gripcontrol/src/response.php';
require 'php-gripcontrol/src/websocketevent.php';
require 'php-gripcontrol/src/websocketmessageformat.php';
require 'php-gripcontrol/src/httpstreamformat.php';
require 'php-gripcontrol/src/httpresponseformat.php';
require 'php-gripcontrol/src/grippubcontrol.php';
require 'php-gripcontrol/src/gripcontrol.php';

Asynchronous Publishing

In order to make asynchronous publish calls pthreads must be installed. If pthreads is not installed then only synchronous publish calls can be made. To install pthreads recompile PHP with the following flag: '--enable-maintainer-zts', (*6)

Also note that since a callback passed to the publish_async methods is going to be executed in a separate thread, that callback and the class it belongs to are subject to the rules and limitations imposed by the pthreads extension., (*7)

See more information about pthreads here: http://php.net/manual/en/book.pthreads.php, (*8)

Usage

Examples for how to publish HTTP response and HTTP stream messages to GRIP proxy endpoints via the GripPubControl class., (*9)

 'https://api.fanout.io/realm/',
        'control_iss' => '',
        'key' => Base64.decode64('')));

// Add new endpoints by applying an endpoint configuration:
$grippub->apply_grip_config(array(
        array('control_uri' => ''), 
        array('control_uri' => '')));

// Remove all configured endpoints:
$grippub->remove_all_clients();

// Explicitly add an endpoint as a PubControlClient instance:
$pubclient = new PubControl\PubControlClient('');
// Optionally set JWT auth: $pubclient->set_auth_jwt(, '')
// Optionally set basic auth: $pubclient->set_auth_basic('', '')
$grippub->add_client($pubclient);

// Publish across all configured endpoints:
$grippub->publish_http_response('', 'Test publish!');
$grippub->publish_http_stream('', 'Test publish!');

// Use publish_async for async publishing only if pthreads are installed:
// $grippub->publish_http_response_async('', 'Test async publish!',
//         null, null, 'callback');
// $grippub->publish_http_stream_async('', 'Test async publish!',
//         null, null, 'callback');
// Wait for all async publish calls to complete:
// $grippub->finish();
?>

Validate the Grip-Sig request header from incoming GRIP messages. This ensures that the message was sent from a valid source and is not expired. Note that when using Fanout.io the key is the realm key, and when using Pushpin the key is configurable in Pushpin's settings., (*10)

<?php
$is_valid = GripControl\GripControl::validate_sig(headers['Grip-Sig'], '<key>');
?>

Long polling example via response headers. The client connects to a GRIP proxy over HTTP and the proxy forwards the request to the origin. The origin subscribes the client to a channel and instructs it to long poll via the response headers. Note that with the recent versions of Apache it's not possible to send a 304 response containing custom headers, in which case the response body should be used instead (next usage example below)., (*11)

'))
    return;

// Instruct the client to long poll via the response headers:
http_response_code(200);
header('Grip-Hold: response');
header('Grip-Channel: ' .
        GripControl\GripControl::create_grip_channel_header(''));
// To optionally set a timeout value in seconds:
// header('Grip-Timeout: ');
?>

Long polling example via response body. The client connects to a GRIP proxy over HTTP and the proxy forwards the request to the origin. The origin subscribes the client to a channel and instructs it to long poll via the response body., (*12)

'))
    return;

// Instruct the client to long poll via the response body:
http_response_code(200);
header('Content-Type: application/grip-instruct');
echo GripControl\GripControl::create_hold_response('');
// Or to optionally set a timeout value in seconds:
// echo GripControl\GripControl::create_hold_response(
//        '', null, );
?>

WebSocket over HTTP example. In this case, a client connects to a GRIP proxy via WebSockets and the GRIP proxy communicates with the origin via HTTP., (*13)

 ''));
        $grippub->publish('', new PubControl\Item(
                new GripControl\WebSocketMessageFormat(
                'Test WebSocket publish!!')));
    }
}

// Validate the Grip-Sig header:
$request_headers = getallheaders();
if (!GripControl\GripControl::validate_sig($request_headers['Grip-Sig'], ''))
    return;

// Set the headers required by the GRIP proxy:
header('Content-Type: application/websocket-events');
header('Sec-WebSocket-Extensions: grip; message-prefix=""');
http_response_code(200);
$in_events = GripControl\GripControl::decode_websocket_events(
        file_get_contents("php://input"));
if ($in_events[0]->type == 'OPEN')
{
    // Open the WebSocket and subscribe it to a channel:
    $out_events = array();
    $out_events[] = new GripControl\WebSocketEvent('OPEN');
    $out_events[] = new GripControl\WebSocketEvent('TEXT', 'c:' .
    GripControl\GripControl::websocket_control_message('subscribe',
            array('channel' => '')));
    $response = GripControl\GripControl::encode_websocket_events($out_events);
    ignore_user_abort(true);
    header("Connection: close");
    header("Content-Length: " . strlen($response));
    echo $response;
    ob_flush();
    flush();
    $publish_message = new PublishMessage();
    $publish_message->start();
}
?>

Parse a GRIP URI to extract the URI, ISS, and key values. The values will be returned in a hash containing 'control_uri', 'control_iss', and 'key' keys., (*14)

<?php
$config = GripControl\GripControl::parse_grip_uri(
    'http://api.fanout.io/realm/<myrealm>?iss=<myrealm>' .
    '&key=base64:<myrealmkey>');
?>

The Versions

02/08 2017

dev-master

9999999-dev https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Konstantin Bokarius

02/08 2017

dev-develop

dev-develop https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Konstantin Bokarius

02/08 2017

2.1.1

2.1.1.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Konstantin Bokarius

23/05 2017

2.1.0

2.1.0.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Konstantin Bokarius

22/04 2015

2.0.0

2.0.0.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Konstantin Bokarius

04/04 2015

1.1.4

1.1.4.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Konstantin Bokarius

19/02 2015

1.1.3

1.1.3.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius

09/02 2015

1.1.2

1.1.2.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius

01/02 2015

1.1.1

1.1.1.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius

01/02 2015

1.1.0

1.1.0.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius

20/01 2015

1.0.2

1.0.2.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius

19/01 2015

1.0.1

1.0.1.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius

19/01 2015

1.0.0

1.0.0.0 https://github.com/fanout/php-gripcontrol

A GRIP library for PHP.

  Sources   Download

MIT

The Requires

 

by Konstantin Bokarius