Functions v1

Functions enable you to write code or leverage existing integrations to address your business needs without spinning up your own servers or incurring an increased latency through call-outs to an external server or serverless technology. The code you write or use runs within PubNub's operational environment, giving you the scale and speed you need for your applications.

PubNub offers Functions in two versions (v1 and v2) that differ in terms of scope, terminology, UI flow on the Admin Portal, and library or REST API access.

Function versions

This document details the UI for Functions v1 that are accessible for PubNub users who created Functions before v2 was introduced. To learn about the differences between both versions, refer to the Functions v1 vs. v2 comparison.

The steps for creating a Function also differ depending on the version. For details, refer to Development Guidelines v1 and Development Guidelines v1.

Modules

To create a function you need to first create a Module

Module

A collection of Functions (v1) used to manage and control a set of Functions.
.

Portal with no modules

To create a Module:

  1. Choose an app with a corresponding keyset.

  2. Click + Create New Module.

  3. Fill in the mandatory Name field and the optional Description field.

For information on how to create Functions, refer to the Development Guide.

Statistics

You can select the module to view all functions that have been created inside of it.

Portal with available functions

At first glance, you can see whether a module is Running or Stopped. You can also see a number of metrics related to functions inside that module:

Functions statistics

NameDescription
TotalThe number of created functions inside the module.
RunningAll functions in the module are running and operational.
StoppedNone of the functions in the module are running. Messages will pass through the PubNub network without triggering your saved Functions.
PendingFunctions in the module are being started. This is a transient state and the module may remain in this state for up to 20 seconds.

Below is the list of states that the Module can be in:

StateDefinition
StoppedNone of the Functions in the Module are running. Messages will pass through the PubNub network without triggering your saved Functions.
RunningAll Functions in the Module are running and operational.
PendingThe Functions in the Module are being started. This is a transient state, and the Module may remain in this state for up to 20 seconds.
StoppingThe Module is being stopped. Like the Pending state, this is a transient state, and the Module may remain in the Stopping state for few seconds.
FailedOne or more Functions in the Module failed to run because of an error, and all Functions are stopped. Check the logs for more information.

Functions overview

This section gives you an overview of all functions that may be available in a module:

Functions overview inside module

NameDescription
FunctionName of your function.
EventEvent type tied to your function.
Channels/Path/IntervalConfiguration parameters of your function.
StateState of your function. Refer to the previous table to view available states.

Third-party services integration

Apart from creating a function by yourself, you can also leverage existing third-party integrations, including but not limited to language translation, sentiment analysis, SMS, text-to-speech, and content moderation.

Clicking the Explore Templates button inside a module opens a limited Functions integrations catalog inside Portal.

Functions catalog

If you cannot find a third-party service that suits your needs, you might want to check out the Integrations catalog outside of the Admin Portal, where you can view all possible integrations. Once you decide on a third-party integration, click Use template and follow the wizard.

Portal functions wizard

Function configuration view

You can click one of the functions inside the module to open the function configuration view. This view gives you the ability to configure your function, change its code as well as rename it, specify which channel the function uses, or test your function. The view is divided into the following sections:

Configure view of the function

Function parameters

You can configure the following parts of your function:

NameConfigurable items
Function nameCurrent name of the function.
Event typeEvent type of the currently selected function.
ChannelChannel the function runs on.
URI pathA suffix that is added to the full URL path at which your On Request function is triggered. For example, if you specify the URI Path as hello, your function will be triggered once you send a request to https://ps.pndsn.com/v1/blocks/sub-key/{your-sub-key}/hello. Only available in the On Request event type.
Test PayloadTest message payload. Its initial structure changes based on the event type of the selected function. You can change the payload to suit your needs.
Add REST parametersAdditional REST parameters. Only available in the On Request event type.
Add headersAdditional headers. Only available in the On Request event type.
IntervalInterval for the function to trigger. Can be set between 10000 and 3600000 milliseconds. Only available in the On Interval event type.
My SecretsCredentials configuration. You can store secrets and use them in the function body.

Code editor

Use it to create or modify the code responsible for your function. The example code differs between function event types and is based on the one you select when you first create a function. If you change the function event type from the drop-down list in the function parameters section, the code will not refresh.

For further information on how to create functions, refer to the Creating a function section.

Wildcard channels

Wildcard channels won't work inside the code editor, you have to provide a specific channel.

Console view

Displays debug information about your module and function. You can show all filters, clear the console, or filter the view by selecting one or more of the following:

Filter nameDisplayed information
Channel statusThe channel your function runs on.
Module statusThe deployment status and deployment regions.
Test messagesType and body of published messages set in the Test payload section.
Console outputThe output logged to the console from within your function body.

Export logs through Events & Actions

Any running function publishes its logs to an internal blocks-output-* channel, like blocks-output-NSPiAuYKsWSxJl4yBn30.

You can find this channel name in the Console output:

Functions log

This channel has a 250-line limit. After exceeding the limit, new logs overwrite the old ones.

If you don't want to lose track of your logs and set up your own limits, you can use Events & Actions to export logs to an external service using the Webhook or S3 action.

To do that, follow these steps:

  1. Set up an event listener to listen for events on the internal channel. Provide the following configuration and enter the correct blocks-output-* channel from the Console output in the Condition value field.

    Export logs through E&A

  2. Create either a Webhook or S3 action and add the created event listener to it.

    When configuring this action, you can use the Batching feature to send multiple events in a single request.

Last updated on