Receiving Client Events via Webhooks

It’s always a good day in the Pusher office when we can announce a new feature for you all to use. This time around we’re happy to unveil our new client_event Webhook, allowing you to be notified on your server as soon as Client Events are sent within a Private or Presence Channel on your application.

The client_event Webhook is a simple but powerful feature and it requires little effort on your part to start using it, just a REST endpoint to have the Client Events sent to.

Why do I need the Client Event Webhook?

If you’ve ever wanted to receive Client Events on your server then this is going to help you out immensely. You no longer have to use a client library on the server to receive messages, instead you can use the Webhook to receive them in a format that makes sense and is easy to manage on your server. Bear in mind that this only applies to messages sent as Client Events.

How is the Webhook data structured?

The data payload within the client_event Webhook is a JSON object, structured like this:

{
  "time_ms": timestamp in milliseconds since epoch,
  "events": [
    {
      "name": "client_event",
      "channel": "name of the channel the event was published on",
      "event": "name of the event",
      "data": "data associated with the event",
      "socket_id": "socket_id of the sending socket",
      "user_id": "user_id associated with the sending socket" # Only for presence channels
    }
  ]
}

Here is an example of a real Webhook response from a Private Channel:

{
  "time_ms": 1396258618139,
  "events": [
    {
      "name": "client_event",
      "channel": "private-test_channel",
      "event": "client-my_event",
      "data": "{"data":1234}",
      "socket_id": "34718.5367461"
    }
  ]
}

If you’re still unsure, our documentation on Webhooks explains the payload data format and processing in a little more detail.

How do I use it?

Pusher will send a client_event event whenever a client event is sent on any private or presence channel, but first you need to set things up.

Step 1: Enable the Client Event Webhook

The first thing you need to do is go into your application dashboard and create a new Webhook for Client Events. Set the WebHook URL to the endpoint on your server that will be receiving the Webhooks.

Step 2: Set up the endpoint on your server

This Node.js example will get you up and running with consuming the new client_event Webhook. Though the following basic process is the same for all platforms:

  1. Set up a REST endpoint for the Webhook (in this case /client_event)
  2. Deconstruct the Webhook payload and process it how you wish

Note: This assumes you are using Express 3. If you’re using Express 4 then you’ll need to use the updated API

// Require Express (HTTP server)
// http://expressjs.com
var express = require("express");
var app = express();
var server = require("http").createServer(app);

// Configure Express server
app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.errorHandler({
    dumpExceptions: true,
    showStack: true
  }));
});

// Webhook endpoint
app.post("/client_event", function(req, res) {
  var timestamp = req.body.time_ms;
  var events = req.body.events;

  console.log(events);

  // Respond with a success code
  res.send(200);
});

// Set up HTTP server to listen on system port, or port 5000
server.listen(process.env.PORT || 5000);

Testing the Webhook using RequestBin

If you’re having trouble getting the Webhook to work, it’s a good idea to test things using RequestBin. To do this you’ll want to set up a new RequestBin and use the given URL as the endpoint for the Webhook on your Pusher dashboard. Anything sent to that URL will show up on RequestBin and will quickly show you whether things are set up correctly.

More soon

This is just one of many new features and improvements that we’re working on. We can’t wait to show you the rest and we’ll keep you updated as we get closer to releasing them.

In the meantime, let us know what you use this new Webhook for!

Ready to begin?

Start building your realtime experience today.

From in-app chat to realtime graphs and location tracking, you can rely on Pusher to scale to million of users and trillions of messages