CtrlK
BlogDocsLog inGet started
Tessl Logo

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.

69

Quality

85%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

Low

Low-risk findings worth noting

SKILL.md
Quality
Evals
Security

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

  1. Identify Function Type: Before Publish, After Publish, On Request, or On Interval.
  2. Design Logic: Plan the transformation, integration, or business logic.
  3. Implement Function: Write async/await code with proper error handling.
  4. Use Modules: Leverage kvstore, xhr, vault, pubnub, crypto modules.
  5. 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 that xhr + pubnub external calls stay within the 3-call cap (KVStore, vault, and pure-CPU helpers do not count — see Quirk 2); ensure proper try/catch wrapping.
  6. Handle Response: Return request.ok() / request.abort() or response.send() appropriately.
  7. Configure Channel Patterns: Wildcard patterns must end with .*, max two literal segments before wildcard.
  8. Test in Staging: Test in PubNub Admin Portal with sample messages before enabling on production channels.
  9. Deploy to Production: Enable on live channel patterns and monitor logs.

Reference Guide

ReferencePurpose
functions-basics.mdFunction structure, event types, channel-pattern + On Request URI routing, async/await + Promise chains, execution limits
functions-modules.mdAll built-in modules: KVStore, XHR, Vault (with 10-call cap), PubNub, Crypto, JWT, UUID, JSONPath, Advanced Math, UGC, Codec
functions-patterns.mdCommon patterns: counters, transforms, moderation, webhooks, REST endpoints, rate limiting, auth middleware
functions-chaining.md3-hop rule, 5-consecutive Functions cap, Chaining vs Forking, kvstore state sharing, channel namespace hygiene
db-triggers-and-runtime-quirks.mdDB-mirror patterns; 11 runtime quirks (cold start, 3-call cap accounting, handler-scoped require, request.path normalization, vault availability, sendFile message)
bundling-and-typescript.mdTypeScript + 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 + pubnub API 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/catch and ensure every code path returns the trigger's completion call (request.ok() / request.abort() / response.send()).
  • Use vault for secrets, never hardcode. Guard vault.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 of shims 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 templates
  • get_sdk_documentation — pull current Function module API references (see intent-to-tool routing)

See Also

Output Format

When providing implementations:

  1. Include complete, working function code.
  2. Show proper async/await with try/catch.
  3. Explain module usage and imports.
  4. Note channel pattern configuration.
  5. Include deployment instructions.
  6. State the function type at the top of every snippet.
Repository
pubnub/skills
Last updated
First committed

Also appears in

pubnub/pubnub-functions
Modified

on Feb 13, 2026

Is this your skill?

If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.