Create real-time updates using Server-sent Events and libSSE

When I first learn node.js, I learned how to use, a node.js module for real-time communication between the client and server. However, when I’m going back coding PHP for a while, I started to figure out only few PHP libraries offered this functions and the techniques they uses are old (AJAX Streaming, Forever <iframe>, etc.). One day I found a new technique called Server-sent Events and it’s pretty easy to implant. I have an idea of creating an interface for Server-sent Events in order to speed up development and make it easier to use. libSSE is the library I’ve create just for this and it’s event-based which means data is sent only when an event happens. It also has some utility classes for communicate with other PHP scripts.

Here’s the library:

It’s pretty easy to use. Here’s an example.

require_once('./src/libsse.php');//include the library

//create the event handler
//every event must inherit the base event class SSEEvent
class YourEventHandler extends SSEEvent {
    public function update(){
        //Here's the place to send data
        return time();
    public function check(){
        //Here's the place to check when the data needs update
        return time() % 20 === 0;

$sse = new SSE();//create a libSSE instance
//register your event handler
    'event_name', //The name of the event
    new YourEventHandler() //The event handler defined above
$sse->start();//start the event loop

On the client-side you need to set up an EventSource object to listen event from server.

var sse = new EventSource('path/to/your/sse/script.php');
    var data =;
    //handle your data here

Remember that Server-sent Events is still a new standard and not all browsers supports it. However, polyfills are available for them. Hope you like it and find it useful.

2 Replies to “Create real-time updates using Server-sent Events and libSSE”

  1. Hello !
    Thanks for lib SSE ! It’s a realy great lib !
    I’m using it for a multi rooms and multi users online HTML5 chat…

    But i often have this error in my app error log :
    2014-09-17 12:53:15 – UNKNOWN ERROR C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\lib\libsse\libsse.php (line 131) [8] ob_flush(): failed to flush buffer. No buffer to flush

    Line 131 of libsse.php is :
    @ob_flush(); (131)
    @flush(); (132)

    I have this error 2 or 3 times per minute 🙁

    How can i avoid this nasty php error ???

    Thanks a lot for your help

    Frederic, France

  2. hi i need help in programming when my data is insert in the sql that time trigger fired and i need to send the data to client using php script

    after trigger fired i need to run php script automatically

    Thank You

Leave a Reply

Your email address will not be published. Required fields are marked *