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.