PubNub FunctionsIntro to the Function

In the paradigm of a Programmable Network, the Function is the entity which contains the program, or logic, that runs against the message.

A Function is made up of many components.

Functions are implemented as JavaScript modules with a default export function, and are defined by default with the structure shown below.

The default function is the logic that will execute for every message sent across each channel that the Function is bound to.

The function accepts as a parameter a single message at a time (within the request parameter's message attribute).

Upon completion of the logic, you must return the request object.

export default (request) => {
    // Your code here
    return request.ok();

In the above example, there is no logic being performed on the message – since we just return the request without modifying the request.message in any way, it's the equivalent of just passing the message through without mutating it.

The request is an envelope object that contains different attributes and a few helper methods.

message:String (Optional)
Returns a promise that resolves to the value of the request object.
message:String (Optional)
Returns a promise that resolves to the value of the request object, with an error code.

All these attributes in the request object are accessible from within the Function and may be used as part of your Function logic. They are all associated with the HTTP request received by the PubNub DSN.

The actual user message sent to the channel.
Method used by the publishing client when sending the message to PubNub.
The Publish Key used when sending the message.
The Subscribe Key to retrieve the message.
The list of channels on which the message was sent on.
API Version.
IP of the publishing client.
Origin defined on the publishing client.
The Publishing client's useragent string.
URL Parameters made on the request.
The URI of the request.

Modules are 3rd-party libraries that enable additional functionality not found as a native part of the Javascript language.

PubNub Functions exposes modules to Functions through the CommonJS require() syntax.

When importing a module, use the require() command. The require command will return an instance of the module which you can call methods from.

const moduleVariableName = require(moduleSuportedByPubNub);

Currently the following modules are supported in PubNub Functions, but more are to come, and if there is a specific module that would help make PubNub Functions more effective for your use case, please email or your PubNub Account Manager so we can prioritize it!

Module NameDescription
Global (lat-long) coordinate and geometry functions.
Provides an encoder for HTTP basic auth credentials.
Provides an encoder and decode for Base64 strings.
Provides an encoder and decoder for URI query parameters.
The Console module mimics the behavior of the console object found in many Javascript frameworks, and is primarily used for logging purposes. Please note when messages output in a console.log() are replicated in the PubNub Data Stream Network, just like messages sent via publish().
The Crypto module allows to perform encryption and decryption operations on messages in an Function.
The State module allows to access the KV store. It is a persistent key value store that acts as a database for your PubNub Functions. This database is globally distributed and eventually consistent. Data in the KV store is shared at a subscribe key level.
The PubNub module allows you leverage some of the other native client SDK features supported by the PubNub Data Stream Network. Currently publish, time, and fire are supported.
Simple utility functions that didn't belong anywhere else.
The XHR module is used to enable the developer to send HTTP or HTTPS requests to a remote web server and load the response data back into a Function. This module is perfect for 3rd party authentication or triggering webhooks from a Function.