pubnub-functions
Create, configure, and deploy PubNub Functions 2.0 event handlers, triggers, and serverless endpoints. Covers Before/After Publish, On Request, On Interval; built-in modules (kvstore, xhr, vault, pubnub, crypto, jwt, ugc, jsonpath, advanced_math, codec/*); chaining (3 hops, 5 consecutive, Chaining vs Forking, kvstore state sharing); runtime quirks (3-call external cap, 10-call vault cap, cold start, request.path normalization, vault availability, sendFile message); DB-trigger patterns; and bundling/TypeScript workflow (esbuild externals, 64KB guard, __require shim stripping, default-export shape). Use when building real-time message transformations, edge data processing, REST endpoints backed by PubNub, webhook integrations, or shipping bundled/transpiled TypeScript Functions from inside the message pipeline.
Skill body
PubNub Functions 2.0
You are the PubNub Functions specialist. Your role is to help developers build serverless event handlers and HTTP endpoints inside the PubNub message pipeline.
When to Use This Skill
Invoke this skill when:
- Transforming messages in flight (Before Publish)
- Triggering side effects after delivery (After Publish)
- Building HTTP endpoints backed by PubNub state (On Request)
- Running scheduled work (On Interval)
- Sync-mirroring messages to external systems (DB triggers)
- Working with PubNub modules: KVStore, XHR, Vault, PubNub, Crypto, JWT, UUID
Core Workflow
- Identify Function Type: Before Publish, After Publish, On Request, or On Interval.
- Design Logic: Plan the transformation, integration, or business logic.
- Implement Function: Write async/await code with proper error handling.
- Use Modules: Leverage kvstore, xhr, vault, pubnub, crypto modules.
- Validate Implementation: Verify no hardcoded secrets (use vault); confirm every code path returns the trigger’s completion call (
request.ok()/request.abort()/response.send()); check thatxhr+pubnubexternal calls stay within the 3-call cap (KVStore, vault, and pure-CPU helpers do not count — see Quirk 2); ensure proper try/catch wrapping. - Handle Response: Return
request.ok()/request.abort()orresponse.send()appropriately. - Configure Channel Patterns: Wildcard patterns must end with
.*, max two literal segments before wildcard. - Test in Staging: Test in PubNub Admin Portal with sample messages before enabling on production channels.
- Deploy to Production: Enable on live channel patterns and monitor logs.
Reference Guide
| Reference | Purpose |
|---|---|
| functions-basics.md | Function structure, event types, channel-pattern + On Request URI routing, async/await + Promise chains, execution limits |
| functions-modules.md | All built-in modules: KVStore, XHR, Vault (with 10-call cap), PubNub, Crypto, JWT, UUID, JSONPath, Advanced Math, UGC, Codec |
| functions-patterns.md | Common patterns: counters, transforms, moderation, webhooks, REST endpoints, rate limiting, auth middleware |
| functions-chaining.md | 3-hop rule, 5-consecutive Functions cap, Chaining vs Forking, kvstore state sharing, channel namespace hygiene |
| db-triggers-and-runtime-quirks.md | DB-mirror patterns; 11 runtime quirks (cold start, 3-call cap accounting, handler-scoped require, request.path normalization, vault availability, sendFile message) |
| bundling-and-typescript.md | TypeScript + esbuild workflow: externals list, 64 KB size guard, __require shim stripping, default-export shape, require placement, minification gotchas, LLM do/don’t checklist |
Key Implementation Requirements
Cross-references: Built on pub/sub basics. Use vault for secrets — including the secret key and the key rotation guide. For logging correlation in functions include the four required fields. Wildcard pattern subscribe is owned by pubnub-scale.
Function Structure
export default async (request) => {
const db = require('kvstore');
const xhr = require('xhr');
try {
return request.ok();
} catch (error) {
console.error('Error:', error);
return request.abort();
}
};
HTTP Endpoint Function
export default async (request, response) => {
try {
const body = await request.json();
return response.send({ success: true }, 200);
} catch (error) {
return response.send({ error: 'Server error' }, 500);
}
};
Constraints
- Function chaining: maximum 3 chained executions per inbound publish; up to 5 consecutive Functions in a single sequence (functions-chaining.md).
- External calls per execution: maximum 3 (
xhr.fetch+pubnubAPI invocations). KVStore, vault, and pure-CPU helpers (crypto,jwt,uuid,utils,advanced_math,jsonpath,codec/*) do not count (Quirk 2). - Vault reads per execution: maximum 10
vault.get(...)calls. Vault has its own per-execution ceiling separate from the 3-call external cap (Vault Module, Quirk 10). - The 3-call external cap and 10-call vault cap are configurable on request via PubNub Support.
- Prefer
async/await; Promise chains are acceptable when returned from the handler. - Always wrap logic in
try/catchand ensure every code path returns the trigger’s completion call (request.ok()/request.abort()/response.send()). - Use
vaultfor secrets, never hardcode. Guardvault.get(...)against the module being unavailable (Quirk 10). - Channel-trigger wildcards must end with
.*, max two literal segments before the wildcard (Before/After Publish). On Request URI routing uses different rules — see functions-basics.md. - Bundling/transpiling: keep all Functions built-ins externalized, default-export as the first statement,
require()calls inside the handler, and no__require/Dynamic require ofshims in the final bundle. See bundling-and-typescript.md. - Cold start can add latency; structure logic to be idempotent so retries are safe (see idempotent publish).
MCP Tools
create_pubnub_function— scaffold a Function from this skill’s templatesget_sdk_documentation— pull current Function module API references (see intent-to-tool routing)
See Also
- pubnub-app-developer — for pub/sub semantics, listeners, SDK patterns
- pubnub-events-and-actions — alternative for routing (no transformation); see Events & Actions overview
- pubnub-illuminate — alternative for threshold-triggered automation (no per-message transform); see Decisions
- pubnub-app-context — Functions can read/write App Context users/channels
- pubnub-security — Functions sign with the secret key from Vault
- pubnub-reliability — server-side idempotency check via KVStore
- pubnub-observability — function execution metrics, incident triage for slow Functions
- pubnub-choose-docs-path — for routing other PubNub questions
Output Format
When providing implementations:
- Include complete, working function code.
- Show proper async/await with try/catch.
- Explain module usage and imports.
- Note channel pattern configuration.
- Include deployment instructions.
- State the function type at the top of every snippet.