2

I am using Cakephp Events and Event Source/Server Sent Events (http://www.w3schools.com/html/html5_serversentevents.asp) for live updates.

From all the controllers, I am emitting events like this :

class MyController extends AppController {
    public function someAction() {
        //........
        $event = new CakeEvent('Controller.MyController.example', $this, array(
            'data' => $someData
        ));
        $this->getEventManager()->dispatch($event);
        //.........
    }
}

And added following line in Config/bootstrap.php :

require_once APP . 'Config' . DS . 'events.php';

And in Config/events.php

App::uses('CakeEventManager', 'Event');
App::uses('MyListener', 'Lib/Event');
// Global Listener
CakeEventManager::instance()->attach(new MyListener());

And in Lib/Event/MyListener.php

App::uses('CakeEventListener', 'Event');
class MyListener implements CakeEventListener {

    public function implementedEvents() {
        return array(
        'Controller.MyController.example' => 'example',  
        );
    }

    public function example(CakeEvent $event) {
        /*Do some optional manipulation with the $event->data,then send the data using event stream.
        How can I call some another Controller to create event stream ?
        (Should I create Event Stream here itself? If yes, how?)
        I know how to create event stream in simple php :

        header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');
        header('Connection: keep-alive');

        echo "data: $someData\n\n";
        flush();
         */
    }
}

How can I create event stream?

PS : I'm using Cakephp events because of it allow me collect required data from different controllers at one place, and then from there, I could create Event Stream (server sent events). If there are any better options, please share.

manish
  • 966
  • 2
  • 11
  • 35

1 Answers1

1

I think this question is far to broad to be answered in a reasonable long text. So I would recommend you to read this question and the first two answers Server-sent events and php - what triggers events on the server? because it's similar and explains SSE very well.

If you understood SSE you'll realize you'll need a php process that keeps running on a loop. The code from the libSSE lib is more or less selfexplaining I think.

class YourEventHandler extends SSEEvent {
    public function update(){
        //Here's the place to send data
        return 'Hello, world!';
    }
    public function check(){
        //Here's the place to check when the data needs update
        return true;
    }
}

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

You could archive the same by implementing this using a CakePHP shell.

However, your app needs to send / update an event to the shell process. One way is to send it to a DB and check the DB table every X seconds for something new to send. This is not very performant but easy to archive.

Another solution is to have another listener that allows you to send an event from your php script to the look that then sends the event data to the subscriber (the http client). You can get this done more or less simple with http://socketo.me/.

Disclaimer: I haven't implemented SSE myself, but the above should do it, you now have the tools and direction. I've implemented Ratchet for fun and learning purpose before.

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66