Streaming Salesforce Platform Events using gRPC and NodeJS

Delivering real time notifications

The New Salesforce Event Bus

The New Salesforce Event Bus
  • Allows to easily scale the service.
  • It adopts micro-services architecture.
  • It will be a Key part of how we’ll deliver new cross-cloud customer experiences.
  • It’s based on gRPC, that allows us, to combine the power of a runtime engine with publish and subscribe in one simple interface.
  • It will also provide a powerful client-driven flow control for event delivery.
Event-Driven Software Architecture

What can it help us do better ?

  • Basically, it eradicates the need for building out a custom CometD Streaming API client and also the need to use different API Interfaces or Protocols for various common tasks we need to do today.
Pub/Sub API: Building Event-Driven Integrations Just Got Even Easier
  • Subscribing to Event Monitoring events and publishing a Platform Event back into Salesforce to restrict a user’s profile when they log into Salesforce after working hours.
  • Subscribing to Change Data Capture events and replicating order data in an external inventory system.
  • Subscribing to a standard Platform Event like AppointmentSchedulingEvent and integrating with Google Calendar to update users’ calendars.

So what’s gRPC ?

  • “RPC” — stands for Remote processing calls”. The concept was introduced already back in the 90’s and has evolved since then.
  • The “g” in “gRPC”, it’s kind of a secret…no one wants to shout it out, but it was originally pioneered by a team at Google (old project aka “stubby”).
  • Today it’s an Open Source project with complete open specs and contributors. (Though development is still primarily executed by Google devs).
API patterns evolution
GraphQL, gRPC or REST? Resolving the API Developer’s Dilemma — Rob Crowley
When GraphQL meets gRPC… 😍
Providing support for multiple Languages and Platforms Architecture (cc cloudcraft.co)
  • Strongly typed schema for request and response — our contract is well defined, kind of like SOAP/WSDL, but much easier to read, scale and maintain with advanced inteli-sense features in IDE.
  • Language definition and Code generator to support different languages.
  • HTTP/2 and gRPC allows a compressed and secured transaction of messages over the wire — its great for IOT and small devices with limited battery life.
  • Bi-directional streaming in my opinion, would be one of the biggest features included in gRPC.
An Architect’s guide to APIs: SOAP, REST, GraphQL, and gRPC

ProtoBuff

  • “Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data — think XML, but smaller, faster, and simpler. ”
  • It’s an Interface Definition Language (IDL) — You define how you want your data to be structured once. You can use special generated source code to easily write and read the structured data. Use it on a variety of data streams and compile it in a variety of languages.
  • Can be used with more than 11 different coding languages.
Connection via gRPC

Streaming a response to our client app

Long polling is quite expensive…
  • Connections can both send and receive data from the client browser.
  • Can transmit both binary data and UTF-8.
  • When connections are terminated, Web-Sockets won’t automatically recover.
  • Some enterprise firewalls with packet inspection have trouble dealing with Web-Sockets.
Websockets
  • Server-Sent Events (SSE) — Updates are pushed (rather than pulled, or requested) from a server to a browser.
  • The connection is kept open (until it receives an instruction to be closed) by calling EventSource.close().
  • Designed to use the JavaScript EventSource API to subscribe to a stream of data in any popular browser.
  • Built-in support for re-connection and event-id and Transported over simple HTTP instead of a custom protocol.
Server Sent Events

Let’s Demo this in Action

  • Been trying to reuse the same code for both apps as much as possible here. gotta love re-usability.
  • You’ll find that the main difference is only the one component that controls the Subscribe and Publish methods.
Salesforce to Localhost and back in real-time
  • The app is built like a messenger chat between 2 users (Salesforce app vs. localhost app). Each user will be able to send a message, that will Publish a Platform Event with some fields to describe the source of the message.
  • All operations will happen within One unified gRPC interface.
  • Events will be pushed to the client app using Server Sent Events (SSE).

--

--

--

Salesforce ISV Technical Solutions Architect

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Preservation

Installing and Configuring Jenkins on Ubuntu 19.10 (Eoan Ermine)

What Makes Flutter the Ideal Choice for iOS App Development?

Writing OKRs in 1h or less

Large Offline Datasets with Apollo Client

Ensuring Safe Self-Driving

Interfacing the HW with ROS (Robot Arm part 1)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yuval Vardi

Yuval Vardi

Salesforce ISV Technical Solutions Architect

More from Medium

Microservices Integration Test under Cloud Native Architecture

CircleCI 60% faster builds: use Xcode DerivedData for caching!

Making Share My Trip Feature much more robust.

SonarQube Pull Request Decoration with GitLab