Back to the module list

Server-Sent Events

The server-sent events are sent to the user's browser. It supports the two modes : events and data only.

Examples

Read the /tmp/dataFile and send a data only message to browser on each new line

$sse = new Domframework\Sse ();
$sse->setBackendFiles ("/tmp/dataFile")
    ->loop ();

Manage the ping (not used message used to keep the connection opened). Send one ping each 30s.

require ("domframework/sse.php");
$sse = new Domframework\Sse ();
$sse->setBackendFiles ("/tmp/dataFile")
    ->setPingTime (30)
    ->loop ();

Read the /tmp/dataFile and each new line is passed into a method which modify it before sending the result to the browser

function eventModifier ($event)
{
  return strtoupper ($event);
}

$sse = new Domframework\Sse ();
$sse->setBackendFiles ("/tmp/dataFile")
    ->setHandlers(__NAMESPACE__."\\eventModifier")
    ->loop ();

In case of events, the name of the event must be used in the setBackendFiles :

$sse = new Domframework\Sse ();
$sse->setBackendFiles ("event1" => "/tmp/dataFile")
    ->loop ();

In case of events, the name of the event must be used in the Handler :

$sse = new Domframework\Sse ();
$sse->setBackendFiles ("event1" => "/tmp/dataFile")
    ->setHandlers(array ("event1" => __NAMESPACE__."\\eventModifier"))
    ->loop ();

The class definition

Class Domframework\Sse

Namespace Domframework

Description

/**
 This class allow to manage Server-Sent Events
 The browser will be connected to a not ending loop. This loop will send
 ping regularly. If the backend value change, it will be sent to the
 browser.
 The backend will be a file for each event name. Each line of the file will
 be sent when it will be added
 See https://developer.mozilla.org/fr/docs/Web/API/Server-sent_events/
 The developper can use a handler to read/modify each event before it is
 send to the user.

Properties

No property available

Methods

final public function loop ()
/**
 This method is called by the user's browser.
 It send the "ping" each X second and send the backend content if it
 is updated.
 Never return !

final public function setBackendFiles ( $files)
/**
 The backend Files to use.
 The backend directory used to store the files must exists and be readable
 If array (namedEvent => filePath, "data" => dataFilePath);
 If string dataFilePath
 @param string|array $files The files to use
 @return $this

final public function setHandlerDataonly ( $handler, $params=null)
/**
 The optional handler to use in DataOnly. Must be callable method
 @param callable|null $handler The handler
 If callable is null, remove the handler for this event
 @param mixed... $params The optional needed parameters

final public function setHandlersEvent ( $handlers, $params=null)
/**
 The optional handler to use in Events. Must be array of callable methods
 @param array $handlers The handlers method, array[event=>callable]
 @param array|mixed $params The parameters of the handlers
 array[event=>callable]
 If callable is null, remove the handler for this event
 @param array|null $params The optional needed parameters
 array(event=>array(params))
 if event=>null is set, remove the parameters for the event

final public function setPingTime ( $pingTime)
/**
 The pingTime to use. Must be positive. If null, the ping is disabled
 @param integer|float $pingTime The time in seconds between two keepalive
 pings