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:
- Go to the Admin Portal select the App for which you want to enable Functions.
- Select the Keyset for which you want to enable Functions.
- In the Keyset configuration, scroll down to the PubNub feature section and turn on the PubNub Functions toggle.
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.
- Go to the PubNub Functions page in Admin Portal by clicking the Functions icon in the sidebar.
- Select the App and the Key in the header.
- Create a Module by entering a name and a description in the square tile.
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:
- Enter a name.
- Choose a Function Event Type (described below).
- Provide a channel name for messages to be published to:
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.
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.
Code your Functions
Below code will be executed against every message received for the predefined channels.
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!