oktopost/tattler-php

PHP Tattler client

1.0.11 2018-08-12 12:18 UTC

README

Build Status Coverage Status License MIT

Send async messages to your users using Tattler

Installation

$ composer require oktopost/tattler-php

Or add to composer.json:

"require": {
    "oktopost/tattler-php": "^1.0"
}

and then run composer update.

Setup

$config = new TattlerConfig();
$tattlerConfig->fromArray([
        'WsAddress'         => 'TATTLER_WEBSOCKET_ADDRESS',
        'ApiAddress'        => 'TATTLER_API_ADDRESS',
        'Namespace'         => 'YOUR APPLICATION_NAME',
        'Secret'            => 'TATTLER_SECRET',
        'TokenTTL'          => 'USER_TOKEN_TTL',
        'DBDecorator'       => new RedisDecorator(),
        'NetworkDecorator'  => new CurlDecorator()
]);

/** @var ITattlerModule::class $tattler */
$tattler = Tattler::getInstance($tattlerConfig);

note: for using redis db decorator you need to install predis

  • TATTLER_WEBSOCKET_ADDRESS - websocket transport address e.g. ws://websocket.domain.tld:80 or wss://websocket.domain.tld:443
  • TATTLER_API_ADDRESS - api address e.g. http://websocket.domain.tld:80 or https://websocket.domain.tld:443
  • YOUR APPLICATION_NAME - namespace for your application. You can use same tattler server with multiple applications.
  • TATTLER_SECRET - same secret that was defined in tattler-server configuration
  • USER_TOKEN_TTL - time in seconds for users auth tokens used with tattler-server

Then create TattlerController available from your website. See example in DummyControllerExample
note: all methods from that controller should response with JSON body

When php configuration is done, include tattler.min.js to your html and initialize tattler

window.tattler = TattlerFactory.create();

Usage

Setup listener in your js code

window.tattler.addHandler('myMessage', 'globalNamespace', function(data){
	alert(data.message);
});

Send payload to all users from php

/** var ITattlerMessage::class $message */
$message = new TattlerMessage();
$message->setHandler('myMessage')->setNamespace('globalNamespace')->setPayload(['message' => 'Hello world']]);

$tattler->message($message)->broadcast()->say();

See more docs in docs/