PubNub Functions Tutorial

Program the PubNub Data Stream Network and build microservices

PubNub Functions is a Function-as-a-Service platform that allows you to program the PubNub Data Stream Network and build your own microservices and webhooks, backed by the power of the PubNub DSN. PubNub Functions handles scaling, global deployment, redundancy and many other operations related work for you seamlessly.

PubNub Functions enables users to quickly and easily spin up microservices in the PubNub Data Stream Network (DSN) without the requirement of any additional supporting infrastructure.

By providing the ability to program the network, PubNub Functions simplifies deploying custom, realtime logic on a reliable, serverless architecture.

With PubNub Functions you can now program the network, executing a variety of operations against your data streams:

  • Fork the original (or create a modified version) message to an alternate.
  • Mutate messages in-flight (e.g. add a new attribute, or modify or delete an existing attribute), altering the final message received by the publish.
  • Filter messages based on some of their attributes, so that only messages with the right values are passed to subscribing clients.
  • Redirect message data to other 3rd party services via web service calls.
  • Aggregate multiple messages and send a single message to subscribing clients.

PubNub Functions provides HTTP endpoints that make building microservices and implementing Webhooks a breeze.

  • Build chatbots with the backing of PubNub’s real-time network.
  • Set up HTTPS endpoints for your Webhooks and integrate other services with PubNub DSN.
  • Build microservices for your application and extend your product.
  • Implement OAUTH for your PubNub apps.

All PubNub Functions code is written in JavaScript, so there's no need to learn a new language. And once the code is written, it's easily deployed to the DSN globally as a microservice.

PubNub provides lifecycle management of your PubNub Functions (run, stop, update) through a simple and intuitive GUI, CLI, or REST APIs.

Get inspired by perusing the PubNub Functions catalog! PubNub is continually developing and maintaining the BLOCKS Catalog, a large collection of predefined and customizable PubNub Functions.

These predefined PubNub Functions provide ready-to-use code that can operate on your own data streams. PubNub is also continually adding partner-based catalog entries, integrating a wide variety of 3rd party APIs to the PubNub Functions ecosystem.

Catalog-based PubNub Functions are available to all PubNub Functions users at no additional cost – use them as-is, or modify them to fit a customized use case.

In this Quick Start, we will lead you through a step-by-step guide of how to create and deploy a Module in the PubNub Data Stream Network.

Just like other features of the Data Stream Network, PubNub Functions must be enabled on a per Keyset basis.

To enable a Keyset:

  1. Go to the Keyset Configuration via the admin portal.
  2. Scroll down to the PubNub feature section and turn on the PubNub Functions toggle.

Voila! You can now create PubNub Functions for this Keyset.

Now that your Keyset is enabled for PubNub Functions, let's create a Module.

  1. Go to the PubNub Functions page by clicking the PubNub Functions icon in side bar.
  2. Create a Module by entering a name and a description in the square tile.
Create Module Fields

 

The name of a Module is a unique identifier scoped by the Keyset it's under.

A Module is used as a container to group related Functions together. Since we just created this Module, it contains no Functions, so we'll create it now.

To create a Function, you can either hit the CREATE button on the left side or the GUIDE ME button and see some examples.

Admin-Dashboard Functions Tab

In the pop up window, configure your Function by:

  1. Entering a name.
  2. Choosing a Function Type.
  3. Providing either channel names or a URI path.

PubNub Functions can be 1 of 4 types and the type of a Function determines when and how a Function will be triggered.

  • Before Publish or Fire
  • After Publish or Fire
  • After Presence
  • On Request

For more information on Function Types, please see Function Types.

Function Types involving Publish or Presence events are triggered by messages sent to one or several channels, and must always have at least one channel defined for it.

For a Function to execute against messages sent to a single channel, enter the channel name in the channel field.

If you adopt a naming convention with 2 levels of depth when specifying your channels, you can subscribe to channels by using wildcards.

For example, if you have several channels named foo.bar1, foo.bar2, foo.bar3, …, foo.barN, your Function can subscribe to all these channels by entering in the channel field: foo.*

For more information on wildcard subscribing see http://www.pubnub.com/knowledge-base/categories/wildcard-subscribe

And now the fun part starts. Coding! At this step, you can write your code logic in standard JavaScript. This code will be executed against every message received against the predefined channels.

Code this Functions

Now that you are ready to start executing your Function, hit the Start Module button at the top right corner and your Module will be deployed around globe in under a second!

Start Module


Ready To Get Started?

Contact Sales