GithubHelp home page GithubHelp logo

isabella232 / salesforce-streams-nodejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pagerduty/salesforce-streams-nodejs

0.0 0.0 0.0 1.56 MB

Reference implementation of a Salesforce Streaming API consumer in Node.js

Home Page: https://blog.heroku.com/reactive-programming-salesforce-data

License: MIT License

JavaScript 100.00%

salesforce-streams-nodejs's Introduction

Salesforce streams with Node.js

This example app displays a feed of changes happening within a Salesforce org.

๐Ÿค๐Ÿšซ๐Ÿ™…โ€โ™€๏ธ Do not connect this app with a production Salesforce org without proper security review. This app receives potentially confidental data from the Salesforce org via Streaming API.

Deploy

Screenshot of the web interface

Architecture

This app is composed of two server-side processes and a web UI:

Diagram: reactive apps with Salesforce streaming

Messages flow from Salesforce into the stream consumer via Bayeux/CometD, and then are pushed into Redis pub/sub so requests from each independent web client may subscribe to them via Server-Sent Events.

The stream consumer is a single, single-threaded process to reliably ingest the ordered stream from Salesforce. Redis acts as a bridge to support scalable processing of the messages by multiple clients. In this example, we use Redis PUBLISH/SUBSCRIBE to send every web client the complete stream. To instead provide a reliable queue of messages for scalable processing, pub/sub could be replaced with Redis LPUSH/RPOPLPUSH/LREM.

This example app uses the Change Data Capture (CDC) stream for Accounts, which must be enabled for each desired object in Salesforce Setup:

Navigate to Salesforce Setup, then Integrations, then Change Data Capture

Local development

Requires

Setup

In your shell terminal, clone this repo to become the working directory:

git clone https://github.com/heroku-examples/salesforce-streams-nodejs
cd salesforce-streams-nodejs

Install Node packages:

npm install

Copy the local dev environment variables template, and then open .env in your editor:

cp .env.sample .env

โœ๏ธ In .env configure Salesforce authentication.

Salesforce

Login to the Salesforce org.

In Salesforce Setup โ†’ Integrations โ†’ Change Data Capture, select which entities (objects) should produce change messages.

For this example app, Account is selected.

Running

The app is composed of two processes, declared in the Procfile. It may be start using the follow commands:

# First run, use -2 replay to get all retained 
# streaming messages from Salesforce.
REPLAY_ID=-2 heroku local

# After that, simply run the web & stream processes
# as declared in Procfile (like Heroku uses for deployment):
heroku local

# Alternatively, run production-style pre-compiled web app
# (requires rebuilding to see changes):
npm run build
NODE_ENV=production heroku local

Demo

โ–ถ๏ธ in a browser view the web UI http://localhost:3000/.

โ–ถ๏ธ in a separate browser window, login to the associated Salesforce org. Create or update entities (example: Accounts) that are configured for CDC events.

๐Ÿ‘€ observe the changes appearing in the web UI.

โšก๏ธโ˜ notice the bolt & cloud emojis in the web UI. The โšก๏ธ indicates the web browser's on-line status, while the โ˜๏ธ indicates the backend Salesforce streaming connection status. These indicators fade out when off-line. Hover over them to see a textual description of the current state.

Testing

Tested with ava:

npm test

Configuration

Configured via environment variables.

For local development, set these values in .env file.

For Heroku deployment, set these values via Config Vars.

Salesforce Authentication

required

Any one of the following authentication methods must be used by setting its variables:

  • Username + password
    • SALESFORCE_USERNAME
    • SALESFORCE_PASSWORD (the password and security token combined without spaces)
    • SALESFORCE_LOGIN_URL (optional; defaults to login.salesforce.com)
  • Existing OAuth token
    • SALESFORCE_INSTANCE_URL
    • SALESFORCE_ACCESS_TOKEN
    • Retrieve from an sfdx scratch org with sfdx force:org:display
  • OAuth client
    • SALESFORCE_URL
      • Must include oAuth client ID, secret, & refresh token
      • Example: force://{client-id}:{secret}:{refresh-token}@{instance-name}.salesforce.com

Runtime Behavior

  • FORCE_API_VERSION
    • Salesforce API version
  • OBSERVE_SALESFORCE_TOPIC_NAMES
    • required
    • the path part of a Streaming API URL
    • a comma-delimited list
    • example: OBSERVE_SALESFORCE_TOPIC_NAMES=/event/Heroku_Function_Generate_UUID_Invoke__e
  • REDIS_URL
    • required
    • connection config to Redis datastore
    • example: REDIS_URL=redis://localhost:6379
    • default: should be set from Heroku Redis add-on
  • REPLAY_ID
    • force a specific replayId for Salesforce Streaming API
    • ensure to unset this after usage to prevent the stream from sticking
    • example: REPLAY_ID=5678 (or -2 for all possible events)
    • default: unset, receive all new events

salesforce-streams-nodejs's People

Contributors

mars avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.