HTML and CSS Reference
In-Depth Information
// Make sure you grab your own Pusher app credentials!
$key = '1507a86011e47d3d00ad';
$secret = 'badd14bcd1905e47b370';
$app_id = '22052';
$pusher = new Pusher($key, $secret, $app_id);
if (isset($_POST['name']) && isset($_POST['message']))
$data = array(
'name' => htmlentities(strip_tags($_POST['name'])),
'msg' => htmlentities(strip_tags($_POST['message'])),
$pusher->trigger('exercise-3-2', 'send-message', $data);
the first two lines turn on error reporting to make debugging easier (you would turn this off in a production site).
Next, we include the pusher php library, define app credentials (don't forget to plug in your own) and instantiate a
new Pusher object, stored in $pusher .
Next, the script checks for the two required form values, name and message , and stores them in an array if they
do exist. the array is then passed to pusher using the trigger method, which triggers the send-message event
on the channel we've named exercise-3-2 . the event data passed to the trigger method will be sent as JsON,
which the library handles for us.
at this point, submitting the form will cause pusher to send an event, but before we can see the effect of this
in our application, we need to add an event handler using Javascript. But first, we can at least use the pusher
Debug Console to manually check that the event is being triggered (see Figure 3-6 ).
Figure 3-6. The triggered event appearing as an API Message in the Pusher Debug Console
Search WWH ::

Custom Search