How to run a Google Cloud Function when a broadcast starts, stops or changes

In this guide we will look at how to programmatically react to broadcast changes, by listening to webhook events.

We will use Google Cloud Functions (opens new window) to run our JavaScript snippets on every event. The code would be very similar on AWS Lambda, Heroku or your custom VPS and the concepts are the same regardless of your choice of programming language.

Let's first create a function. Log in to the Google Cloud console (opens new window), select or create a project, then create a new Cloud Function.

Give it a descriptive name and ensure trigger is HTTP. We'll use the inline editor for now, since our function will be really short.

Optional async/await support

Node.js 6 is the current default runtime for Google Cloud functions. We'll pick Node.js 8 instead here, since that allows us to use the async/await syntax. You can stick with Node 6 if you like, as long as you avoid using the async and await keywords.

As you can see, by choosing HTTP as trigger, our Google Cloud Function gets a public URL.

A GET request in the browser generates the default output. Yay!

A POST request reaches our function too!

The default hello world function, short as it is, also demonstrates how you can send a custom payload to the function. Let's try that:

curl -i -XPOST -H 'Content-type: application/json' -d '{"message": "a custom message"}'

This is actually pretty close to how webhooks operate, not much more to it: When a broadcast is created or updated, Bambuser's webhook servers will send a POST request with a JSON payload to all registered listener urls.

To be able to see what our webhook server receives on an actual broadcast event, let's use console.log() for now, since the response body we just observed is returned back to the webhook server, and is not visible to us. With Google Cloud functions, console.log() writes to an easily consumable event log available via the web console and in various other ways (opens new window).

While at it, let's rename the function to something more descriptive. Make sure Function to execute is updated too.

exports.broadcastUpdate = (req, res) => {
  let payload = req.body;
  console.log('got incoming broadcast update', {payload});
  res.status(200).send({ok: true});

To get some real-world data, let's register our function as a listener on Bambuser dashboard's Developer Page (opens new window).

Next, click "Test" in Bambuser Dashboard and click "View logs" in the Google Cloud console.


That payload was not too exciting though... Let's create a real broadcast and see what happens. Use Bambuser's mobile app or the "go live" button in Bambuser Dashboard or any other broadcasting approach you prefer.

That's more like it!

During a typical broadcast lifecycle you can expect one add action and several update actions. When a live broadcast ends you should see at least one update where type has the value archived.

What's next?

You now have the ability to perform any custom action you like in near-realtime, perhaps maintaining a database, sending emails to followers or cross-posting a link on Twitter or Slack.

For a more detailed example, see how this function can be extended to post to WordPress every time a new broadcast is created.

Also be sure to check out the REST API:s (opens new window) as well, which lets you query the broadcast database on demand as well as update and remove content.