Agent Skill · PubNub

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.

Provider: PubNub Path in repo: pubnub-functions/SKILL.md

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:

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

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

MCP Tools

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.

Skill frontmatter

license: PubNub metadata: {"author" => "pubnub", "version" => "0.3.0", "domain" => "real-time", "triggers" => "pubnub, pubnub functions, functions, serverless, edge, kvstore, webhook, transform, event handler, real-time functions, message processing, before publish, after publish, on request, on interval, function chaining, function bundling, typescript, esbuild, vault, request path, sendFile, ugc, jsonpath, advanced_math, on request routing", "role" => "specialist", "scope" => "implementation", "output-format" => "code"}