{"library":"salesforce-pubsub-api-client","title":"Salesforce Pub/Sub API Client","description":"The `salesforce-pubsub-api-client` is a Node.js client library for interacting with the Salesforce Pub/Sub API, a gRPC-based event streaming service. Currently at stable version 5.5.2, it is actively maintained with frequent minor and patch releases, addressing bug fixes, dependency updates, and feature enhancements. The client abstracts the complexities of gRPC communication, offering multiple authentication flows (username/password, JWT bearer, client credentials), schema management for event data, and features like batch publishing and robust subscription management with replay IDs and flow control. A key differentiator in version 5 is its shift from an `EventEmitter`-based event handling model to a synchronous callback system, ensuring ordered event processing, and a simplified configuration approach via constructor options.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install salesforce-pubsub-api-client"],"cli":null},"imports":["import { PubSubApiClient } from 'salesforce-pubsub-api-client';","import type { SubscribeCallback } from 'salesforce-pubsub-api-client';","import type { Configuration } from 'salesforce-pubsub-api-client';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { PubSubApiClient } from 'salesforce-pubsub-api-client';\nimport type { SubscribeCallback, SubscriptionInfo } from 'salesforce-pubsub-api-client';\n\n// Ensure these environment variables are set for authentication\nconst CLIENT_ID = process.env.SF_CLIENT_ID ?? 'YOUR_SALESFORCE_CLIENT_ID';\nconst CLIENT_SECRET = process.env.SF_CLIENT_SECRET ?? 'YOUR_SALESFORCE_CLIENT_SECRET';\nconst USERNAME = process.env.SF_USERNAME ?? 'your.email@example.com';\nconst PASSWORD = process.env.SF_PASSWORD ?? 'YOUR_SALESFORCE_PASSWORD';\nconst LOGIN_URL = process.env.SF_LOGIN_URL ?? 'https://login.salesforce.com'; // Or https://test.salesforce.com\n\nasync function main() {\n    const client = new PubSubApiClient({\n        instanceUrl: LOGIN_URL,\n        auth: {\n            username: USERNAME,\n            password: PASSWORD,\n            clientId: CLIENT_ID,\n            clientSecret: CLIENT_SECRET\n        },\n        isDebugLogEnabled: true // Enable debug logging\n    });\n\n    // Define the callback function for incoming events and lifecycle events\n    const subscribeCallback: SubscribeCallback = (\n        subscription: SubscriptionInfo,\n        callbackType: 'data' | 'unsubscribing' | 'error' | 'reconnecting',\n        data: any\n    ) => {\n        if (callbackType === 'data') {\n            console.log(`Received event on ${subscription.topicName}:`, JSON.stringify(data, null, 2));\n        } else if (callbackType === 'error') {\n            console.error(`Subscription error on ${subscription.topicName}:`, data);\n        } else {\n            console.log(`Subscription lifecycle event on ${subscription.topicName}: ${callbackType}`);\n        }\n    };\n\n    try {\n        await client.connect();\n        console.log('Connected to Salesforce Pub/Sub API.');\n\n        // Subscribe to a Platform Event or Change Data Capture topic\n        const topicName = '/event/My_Platform_Event__e'; // Replace with your topic\n        await client.subscribe(topicName, subscribeCallback);\n        console.log(`Subscribed to topic: ${topicName}. Waiting for events...`);\n\n        // Keep the process alive to receive events\n        process.on('SIGINT', async () => {\n            console.log('Disconnecting from Pub/Sub API...');\n            await client.disconnect();\n            console.log('Disconnected. Exiting.');\n            process.exit(0);\n        });\n\n    } catch (error) {\n        console.error('Failed to connect or subscribe:', error);\n        await client.disconnect();\n    }\n}\n\nmain().catch(console.error);","lang":"typescript","description":"Demonstrates how to connect to the Salesforce Pub/Sub API, authenticate using username/password flow, and subscribe to a platform event topic using the v5 synchronous callback model.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}