pubnub

The pubnub built-in module is available only inside a PubNub Function. It provides PubNub client SDK capabilities and Functions-specific publishing methods.

1const pubnub = require('pubnub');

Full parameter and response documentation for each capability lives on its own canonical page:

CapabilityRepresentative methodsCanonical reference
Pub/Subpublish(), fire(), signal()Publish overview. Also documented in full below
Access ManagergrantToken(), parseToken()Access control overview
PresencehereNow(), whereNow(), setState(), getState()Presence overview
Channel GroupsaddChannels(), listChannels(), removeChannels(), deleteGroup()Channel Groups API reference
Message Persistencehistory(), fetchMessages(), deleteMessages(), messageCounts()Message history overview
App Contextobjects.* methodsMetadata overview
Message ActionsaddMessageAction() and related methodsMessage actions overview
Mobile Pushpush.addChannels() and related methodsMobile Push API reference
File SharinglistFiles(), getFileUrl(), publishFile()Files overview

The legacy grant() (Access Manager v2) method is out of scope. grantToken() supersedes it.

File Sharing methods from the pubnub module cover file metadata operations only. Uploading and downloading file content requires a PubNub client SDK. A Function cannot perform the transfer itself.

The following capabilities are Functions-specific and are not documented elsewhere.

Publish, fire, and signal​

publish({message, channel}) sends a message to subscribers and Functions. fire({message, channel}) triggers Functions without delivering to subscribers.

* required
ParameterDescription
message *
Type: Object
The message to send.
channel *
Type: String
The channel to publish to.

Both return a Promise. The published or fired payload must be valid JSON. A string payload doesn't trigger Functions.

1const pubnub = require('pubnub');
2pubnub.publish({
3 channel: 'hello_universe',
4 message: request.message
5}).then((publishResponse) => {
6 console.log(`Publish Status: ${publishResponse[0]}:${publishResponse[1]} with TT ${publishResponse[2]}`);
7});

signal({message, channel}) sends a signal to subscribers and Functions.

* required
ParameterDescription
message *
Type: Object
The signal payload.
channel *
Type: String
The channel to signal on.

Returns a Promise. Signal payloads are limited to 64 bytes. The payload must also be valid JSON. A string payload doesn't trigger Functions.

1const pubnub = require('pubnub');
2pubnub.signal({
3 channel: 'hello_universe',
4 message: request.message
5}).then((signalResponse) => {
6 console.log(`Signal Status: ${signalResponse[0]}:${signalResponse[1]} with TT ${signalResponse[2]}`);
7});

Functions support chaining up to 3 hops, so a Function can trigger at most two more Functions downstream. Trigger each hop using publish(), fire(), or signal().

Was this page useful?

Last updated on