HTML and CSS Reference
In-Depth Information
Implementing Realtime on the Back End
To get the ball rolling with realtime, the events need to be created and triggered on the back end of the app.
Creating the Event
Because of the way the app handles form submissions—all of them are passed through a single form handler
method—sending realtime event notifications takes only a few lines of code, which will be added to the
handle_form_submission() method in the abstract Controller class.
Open system/core/class.controller.inc.php and insert the following bold code:
protected function handle_form_submission( $action )
{
if ($this->check_nonce()) {
// Calls the method specified by the action
$output = $this->{$this->actions[$action]}();
if (is_array($output) && isset($output['room_id'])) {
$room_id = $output['room_id'];
} else {
throw new Exception('Form submission failed.');
}
// Realtime stuff happens here
$pusher = new Pusher(PUSHER_KEY, PUSHER_SECRET, PUSHER_APPID);
$channel = 'room_' . $room_id;
$pusher->trigger($channel, $action, $output);
header('Location: ' . APP_URI . 'room/' . $room_id);
exit;
} else {
throw new Exception('Invalid nonce.');
}
}
A new Pusher object is created and stored in the $pusher variable, then a channel for the room is created using
its ID. Using the action name as the event name, a new event is triggered on the room's channel using the trigger()
method that sends the output array for client-side use.
 
Search WWH ::




Custom Search