Skip to content
Example subscribing and publishing Platform Events in nodejs using jsforce. For https://www.meetup.com/Nashville-Salesforce-Developer-User-Group/events/239966938/
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
images updated readme Jul 20, 2017
public/js added nodejs app to subscribe and publish events Jul 20, 2017
views added nodejs app to subscribe and publish events Jul 20, 2017
.gitignore updated readme Jul 20, 2017
LICENSE BSD 3-Clause License Aug 26, 2017
Procfile added nodejs app to subscribe and publish events Jul 20, 2017
README.md updated readme Jul 20, 2017
app.js updated readme Jul 20, 2017
package-lock.json added nodejs app to subscribe and publish events Jul 20, 2017
package.json

README.md

Swag Level Platform Events Demo

Overview

Example subscribing and publishing Platform Events in nodejs using jsforce, humorously to know when swag levels get dangerously low 😉

Demo app created for the Nashville Salesforce TrailheaDX meetup.

screen shot

Getting Started

  1. Ensure Node.js 8.2.0 or greater is installed on your machine

  2. Create a connected app and enable OAuth Settings

screen shot

  1. In Salesforce Setup, navigate to Platform Events

  2. Create a new event object named "Swag Level" (Swag_Level__e) with one custom text field named "Level" (Level__c)

  3. Checkout this GitHub project locally

  4. In the root folder of the project, create a text file named .env with the following properties:

NODE_ENV=development

# ==========================================
# Salesforce OAuth
# ==========================================

OAUTH_SALESFORCE_LOGIN_URL=https://login.salesforce.com
OAUTH_SALESFORCE_CLIENT_ID=your_connected_app_client_id
OAUTH_SALESFORCE_CLIENT_SECRET=your_connected_app_client_secret
OAUTH_SALESFORCE_REDIRECT_URI=http://localhost:8080/oauth2/callback

# ==========================================
# Salesforce
# ==========================================

SALESFORCE_API_VERSION=40.0

  1. From command line, navigate to the root folder of the project and run the command node app.js

screen shot

  1. In your browser, navigate to http://localhost:8080 (you'll be prompted to login with your Salesforce credentials via oauth)

screen shot

  1. When the page loads, click the Publish Event link to cause a new Swag_Level__e record to be published.

screen shot

  1. In your command console you should see nearly immediately a message that the event has been received.

screen shot

Next Steps

Using the Node.js and Salesforce API is not the only way to subscribe or publish to Platform Events. You can also subscribe to events via an Apex Trigger configured for the after insert trigger event on your __e object, as you would for any other sobject.

You can also publish new events similarly to how you would create any other sobject, just insert a new __e record. You can even publish events via Process Builder or Flows since they can insert records too.

Note, although the look and feel of Platform Event objects is like SObjects (that's intentional for easier adoption), they are not data like your SObject records like accounts and contacts. If your regular Salesforce transaction rolls back, any events you've published are not rolled back.

Resources

You can’t perform that action at this time.