Create a Function
In this tutorial, you will create a Before Publish Function that modifies messages on a channel, deploy it to a keyset, and confirm it is running by publishing a test payload in the Debug console.
What you will build
By the end of this tutorial, your Function will intercept every message published on the hello-world channel and add hello: 'world!' to the payload before PubNub delivers it to subscribers. You will verify this transformation in the Admin Portal, without writing any client code.
Before you start
Before starting, confirm you have:
- A PubNub account with at least one keyset. If you do not have one, see Set up your account.
- The Functions developer role, or Account Admin access. Without one of these, you cannot create Packages or manage deployments.
Every PubNub plan includes Functions.
Create a Package
A Package groups related Functions. You need one before you can create a Function.
- Log in to the Admin Portal and go to Functions.
- Click Create package.
- Choose Configure your own.
- Enter a name and description for the Package, then click Next.
PubNub creates your Package and takes you to the Function creation screen.
Create a Function
- Enter a name for the Function.
- Select Before Publish or Fire as the event type.
- Set the trigger channel to
hello-world. - Click Create package.
PubNub creates the Package with your Function inside. You will see an initial Revision in the Package list.
For more on trigger types and when to use each one, see Functions.
Add your code
-
Click Edit code or Edit Latest on the Package Revision list to open the editor.
-
The editor shows a sample Function. Replace all of it with the following code:
export default async (request) => {
console.log('request', request);
request.message.hello = 'world!'; // augment hello = world
console.log('The message: ', request.message, ' was published on ', request.channels[0], ' via', request.meta.clientip);
return request.ok(); // An async handler always returns a Promise
} -
Click Save. PubNub creates a new Revision. Enter a Revision name and description, then save.
This Function logs the incoming request, adds hello: 'world!' to the message payload, and calls request.ok() to pass the modified message on for delivery.
For details on the request object and handler structure, see Functions development.
Deploy to a keyset
A Revision must be deployed to a keyset before it processes live traffic.
- Click Deploy on the Revision.
- Click Add keyset and select the keyset where you want the Function to run.
- Click Create.
- Click Deploy to confirm. For this example, skip the secrets step if you have no secrets configured.
Your Function is now active on the selected keyset.
Test in the Debug console
- Click Edit code or Edit Latest on the Package Revision list to open the editor.
- Switch to the Debug tab.
- Click Publish in the Test payload section.
You will see log output appear in the console. The response shows your message with the hello: 'world!' field added. This confirms the Function intercepted the message and transformed it before delivery.
What you built
You created a Before Publish Function that adds hello: 'world!' to every message on the hello-world channel, saved it as a Revision, deployed it to a keyset, and verified the transformation in the Debug console. Any client publishing to hello-world on this keyset now has the field added before subscribers receive the message.