Leveraging JWT Authentication for Real-time Salesforce Event Streaming With NodeJS

SoomjeetSahoo
3 min readAug 5, 2023

In today’s fast-paced business landscape, the ability to access and process data in real-time is paramount. Salesforce, a leading customer relationship management (CRM) platform, offers powerful streaming capabilities that allow you to receive and react to data updates as they happen. In this article, we’ll explore a real-world use case where we harness the potential of JSON Web Token (JWT) authentication to establish a secure and efficient connection with Salesforce for real-time data streaming.

Prerequisites: Before delving into the example, make sure you have the following prerequisites:

  1. Node.js installed on your local machine.
  2. An active Salesforce account with appropriate permissions.
  3. A Salesforce Connected App configured to enable JWT token generation.
  4. Basic familiarity with Node.js, JWT, and Salesforce concepts.

Use Case: Notifying customers on Opportunity updates via 3rd party mobile applications.

Picture yourself as an Experience Manager overseeing an Ecommerce platform. In the realm of customer transactions, whenever a shopper makes purchases from your online store, any modifications to their orders necessitate immediate notification through their custom-built mobile application. Through the establishment of a real-time data streaming solution, you can guarantee that each time an alteration occurs within the Order record, the mobile app’s interaction mechanism promptly alerts the customer regarding the pertinent updates to their order status.

Step 1: Getting Set Up

  1. Clone the GitHub repository containing the sample code:
git clone https://github.com/soomjeet/JWTStreamerOnJS.git
cd JWTStreamerOnJS

2. Install the necessary dependencies:

npm install

3. Create a .env file in the project directory and input your Salesforce Connected App details and private key file location:

CONSUMER_KEY=your_salesforce_connected_app_consumer_key
USERNAME=your_salesforce_username
TEST_URL=https://your-salesforce-instance-url
CHANNEL_URL=/event/Your_Channel_Name
PEM_FILE_LOCATION=path_to_your_private_key.pem

Step 2: Unveiling the Code

Let’s break down the key segments of the index.js file, where the magic happens:

  • Importing Dependencies: Essential libraries such as Axios, JSforce, and Moment are imported to facilitate various functionalities.
  • Generating JWT Token: The private key is extracted from the specified PEM file, and a JWT token is generated using your Connected App credentials.
  • Obtaining OAuth Token: The JWT token is leveraged to request an OAuth token, which ensures secure access to your Salesforce instance.
  • Setting Up Real-Time Streaming: With the OAuth token in hand, a connection to Salesforce is established. A streaming extension is created, and a Faye client is initialized to listen for updates on the predefined channel.

Step 3: Running the Code

Execute the code using the following command:

node index.js

As the code executes, your Node.js application becomes an attentive listener for real-time updates on the specified Salesforce channel.

Benefits of JWT Authentication

JSON Web Token (JWT) authentication offers several advantages:

  • Security: JWTs are digitally signed, providing a secure means of transmitting data between parties.
  • Efficiency: JWTs are compact and self-contained, reducing the overhead of token verification.
  • Scalability: Streaming can handle large volumes of data without sacrificing performance.
  • Real-time Insights: React to brand mentions in real time, enabling rapid response to customer orders or engagement opportunities.

Conclusion

In conclusion, our use case demonstrates the impact of real-time data streaming when it comes to Ecommerce application. By embracing this technology, we’ve orchestrated an environment where every order modification triggers a cascade of instant notifications, bridging the gap between the virtual storefront and the customers’ fingertips.

--

--

SoomjeetSahoo

Salesforce Dev @ Salesforce with lots of inquisitiveness to learn and grow in Salesforce and Node Domains. Currently 15x Salesforce Certified & 14x Super Badge.