PubNub Functions gives you a serverless environment to execute functions on the edge: transforming, enriching, and filtering messages as they route through the PubNub network.
PubNub Functions enable you to apply business logic to messages at the edge of the network. Quickly build microservices and incorporate logic including re-routing, augmenting, filtering, transforming, and aggregating data. PubNub Functions are serverless, so there is no need to worry about deploying, maintaining, or scaling server infrastructure.
Some code and use cases in this documentation require add-on features, which you can enable via the PubNub Admin Dashboard.
Example Use Cases
With PubNub Functions you can execute a variety of operations against your data streams. Some examples include:
- Chatbots run efficiently as Functions on PubNub's servers.
- Language translation mutates messages in-flight, altering the final message received by users to be in their requested language.
- Message moderation filters messages based on some of its attributes, so that only messages with acceptable content are passed to subscribing clients.
- Message routing forks the original message (or a modified version) to an alternate destination, such as to external storage.
- User blocking discards messages from specific users.
- Spam filtering detects spam messages and trashes them before they're sent to other users or recorded in history.
- Aggregation logic combines multiple messages into a single message to subscribing clients. For example, in large chat rooms, it can be more efficient to aggregate reactions or live poll results.
- Third-party integrations let you redirect message data to third-party services via web service calls.
The BLOCKS Catalog
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.
There are four Function types:
- Before Publish or Fire functions handle messages before they're published (synchronous)
- After Publish or Fire functions handle copies of messages after they're published (asynchronous)
- After Presence functions receive notifications of presence events, and act on them (asynchronous)
- On Request functions define REST API endpoints
Synchronous (blocking) function types, such as Before Publish or Fire, operate on the original message, before it's delivered. The logic of the Function must complete before the message is released to continue its trip to the subscribers, which introduces a delivery delay (from microseconds for simple arithmetic, to potentially hundreds or thousands of milliseconds if interacting with a 3rd-party REST service).
Asynchronous (non-blocking) function types, such as After Publish or Fire, operate on a copy of the message, and don't increase delivery times. Asynchronous functions can't change the original message, the message received by the client is always identical to the message that triggered the Function.
The following sections describe these function types in greater detail.
Before Publish or Fire functions
These functions are synchronous (blocking).
Use a Before Publish or Fire function if you want to change a message in-flight (add a new attribute or modify an existing attribute). Some applications of Before Publish or Fire include:
- Translating a message from one language to another
- Conversion of values from Imperial to Metric
- Detecting (and censoring) profanity
- Detecting (and normalization) of missing or illegal values
After Publish or Fire functions
These functions are asynchronous (non-blocking).
Use an After Publish or Fire function if you want to pass the original message through the PubNub Data Stream Network intact, and also use it as a trigger to execute other logic (such as logging and message teeing). Some applications of After Publish or Fire include:
- Asynchronous third-party message logging (such as ElasticSearch)
- Mentions, keywords, and other string triggers
- Message teeing and forwarding
After Presence functions
These functions are asynchronous (non-blocking), and trigger on subscriber presence events, not messages.
Use an After Presence function type if you want to pass a presence event through the PubNub Data Stream Network intact, and also use it as a trigger to execute other logic (such as logging and message teeing). Some applications of After Presence include:
- Sending an SMS to an offline user when a member of their priority buddy-list comes online
- Updating an on-hold wait time KV store as JOIN and LEAVE events occur
- Monitor a user's online/offline time card as they JOIN and LEAVE an app
On Request functions
On Request functions let you create publicly accessible REST API methods.
In addition to providing a lightweight approach to publicly accessing your PubNub code, you can also use on-request functions in your other functions as a replacement for
pubnub.fire(). This offers several advantages, such as:
- Breaking your main function into smaller components for ease of maintainability
- Defining a utility function that you'll call from many other functions
- Returning data to the calling function for any number of reasons
Creating a new Function
The code in this section calls
vault.get() to retrieve a subscribe key (
sub_key) stored in your module's secret store. Refer to the Vault Module documentation for more information.
Follow these instructions to create a Before Publish or Fire function that calls an On Demand function:
Go to the application instance on your PubNub Admin Dashboard. Or, create a new application.
Create a new module, and then create a new Before Publish or Fire function.
Configure the function to trigger on specific channels, or use
*to run it on all channels. The different function types are listed in Function types.
Copy your module's subscribe key.
Click My Secrets, and create a
sub_keyentry with a value of your subscribe key. Click Save, and close the panel.
Write the code for your function.
The following sample calls a trivial On Request function:
Create a second function. This one should be an On Request function, listening on the path
The On Request function's code is as follows:
Click the red Save button on the left, and then click Start Module on the right.
Test your function with a sample payload.