Use Functions to manipulate, transform, content filter or backup your data flowing through the PubNub Network

Functions Quickstart

Functions is a JavaScript-based "serverless" environment that lets you execute business logic on the PubNub network. Use Functions to manipulate, transform, content filter or backup your data flowing through the PubNub Network.

Functions Quickstart

1

Turn on PubNub Functions for your Keyset

In order to run PubNub Functions you must enable Functions for your key set first.

To enable a Functions on a Keyset:

  1. Go to the Admin Portal select the App for which you want to enable Functions.
  2. Select the Keyset for which you want to enable Functions.
  3. In the Keyset configuration, scroll down to the PubNub feature section and turn on the PubNub Functions toggle.

executing code against every message received for the predefined channels
You can now create PubNub Functions for this Keyset.

2

Create a Module

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

  1. Go to the PubNub Functions page in Admin Portal by clicking the Functions icon in the sidebar.
  2. Select the App and the Key in the header.
  3. Create a Module by entering a name and a description in the square tile.
Creating a Module

3

Create and Configure Functions

To create a new Function, you can either hit the Create Function button or the guide me button. Or you can click on Import Block to import existing functions from our Blocks Catalog.

In the pop up window, create your Function by:

  1. Enter a name.
  2. Choose a Function Event Type (described below).
  3. Provide a channel name for messages to be published to:
Creating a Module


Event Types

There are 6 events that can trigger a Function, Before Publish or Fire, After Publish or Fire, Before Signal or Fire, After Signal or Fire, After Presence and On Request. For more information on Function Types, please see Function Types.

For the purpose of this exercise we will focus on On Before Publish.

Channels

Channel name is required for Publish, Signal and Presence events as these events are triggered by messages sent to one or more channels. To subscribe to multiple channels you can use wildcards.

4

Code your Functions

You can write your code logic in standard JavaScript. PubNub provides a rich set of built-in libraries including an embedded PubNub library that you can take advantage of. See what you can do with these libraries here.

Below code will be executed against every message received for the predefined channels.

Creating a Module

5

Deploy and Run!

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 the globe in minutes!

6

Next Steps

Visit PubNub Tutorials Browse through our PubNub BLOCKS catalog to see what predefined, customizable Functions are available for you!

Visit PubNub Tutorials to more detailed step by step Functions Tutorial.

Try PubNub Today

Try Our APIs
  • 1M Transactions
  • 1GB Data Persistence
  • All major Features