Skip to content
licson0729 edited this page Apr 6, 2013 · 7 revisions

Documentation of libSSE

This is the documentation of libSSE. It explains how to use libSSE and shows all classes available.

Class: SSE

class SSE {
    public $exec_limit = 600;
    public $sleep_time = 0.5;
    public $client_reconnect;
    public $allow_cors = false;
    public $keep_alive_time = 300;
    public $use_chunked_encoding = false;
    
    public function __construct();
    public function addEventListener($event_name,$handler);
    public function removeEventListener($event_name);
    public function start();
}

SSE::$exec_limit

Default: 600 seconds

This is used to control the execution time of the script in order to save memory and CPU load. If you want to allow the script to run as long as possible, set this to 0.

SSE::$sleep_time

Default: 0.5 seconds

The sleep time after each check for updates. Larger number means the data will be updated with more delay.

SSE::$allow_cors

Default: false

Whether to allow cross domain access or not. If you're going to provide a real-time API to others or you're working with sub-domains you MUST set this to true.

SSE::$keep_alive_time

Default: 300 seconds

The time interval of sending a comment signal to keep the connection alive. Recommend ranges are 5-15 minutes (300-900 seconds).

SSE::$use_chunked_encoding

Default: false

Whether to use chunked encoding or not. Some server may send this header automatically and turning this on may get errors so make sure your server has no problem when turning this on.

SSE::$client_reconnect

Default: 1

The time for the client to reconnect after the connection has been broken.

SSE::addEventListener($event_name,$handler)

Parameters

  • $event_name - The event name
  • $handler - The handler of the event, must be an instance of SSEEvent

Adds a event handler of the event named $event_name. When data is available from the handler libSSE will send them automatically.

SSE::removeEventListener($event_name);

Parameters

  • $event_name - The event to remove

Removes the event handler specified by $event_name.

SSE::start()

Start the whole event loop and sending of data.

Class: SSEEvent

class SSEEvent {
    public function check();
    public function update();
}

This is the base class for all events. All events should inherit this base class.

SSEEvent::check()

This is the method that will be called by libSSE regularly to check for updates for the specified event.

SSEEvent::update()

This is the method that will be called by libSSE to get the event data when an update happens.

Class: SSEData

class SSEData {
    public function __construct($method,$credinals);
    public function get($key);
    public function set($key,$value);
    public function delete($key);
    public static function register($name,$class);
}

This class provides a simple key-value storage to allow cross-script communication.

SSEData::__construct($method,$credinals);

Parameters

  • $method - The method of storage, avaliable options are mysql, mysqli, file and apc
  • $credinals - Extra information needed by a specific method of storage. Default: null

SSEData::get($key)

Parameters

  • $key - The key to the corresponding value

Get the corresponding value by a key specified.

SSEData::set($key,$value)

Parameters

  • $key - The key of the corresponding value
  • $value - The value

Set a value with a key and value.

SSEData::delete($key)

Parameters

  • $key - The key to the corresponding value

Deletes a value with a corresponding key.

The documentation is not completed yet. You can help me finish it if you want.