How to Integrate Salesforce Real-time Events with Spring Boot Using Pub/Sub API

Nagendra SinghNagendra Singh
3 min read

Introduction

Real-time data integration isn't just a nice-to-have anymore—it's essential for today's businesses. The Salesforce Pub/Sub API marks a big change from the old polling methods to fast, event-driven systems. In this guide, we'll dive into the technical details of the Salesforce Pub/Sub API and show you how to securely integrate it using the OAuth 2.0 Client Credentials flow with a hands-on Spring Boot example.

What You'll See

  • High level understanding of Salesforce Pub/Sub API

  • Understanding gRPC, Protocol Buffers, and Avro schema evolution

  • Implementing OAuth 2.0 Client Credentials for enterprise security

  • Live Demo with Integration with Spring Boot

Salesforce Pub/Sub API: The Technical Foundation

The Salesforce Pub/Sub API is a high-performance, gRPC-based streaming platform that enables real-time event delivery at enterprise scale. Built on modern protocols, it supports:

Core Capabilities

  • Platform Events: Custom business events with schema validation

  • Change Data Capture (CDC): Real-time database change notifications

  • Real-time Event Monitoring: System-level event tracking

  • High Volume Platform Events: Enterprise-scale event processing

Understanding the Pub/Sub API Protocol Stack

gRPC and Protocol Buffers

Salesforce Pub/Sub API uses gRPC (Google Remote Procedure Call) with Protocol Buffers for optimal performance:

message FetchRequest {
  string topic_name = 1;           // e.g., "/event/LogEvent__e"
  ReplayPreset replay_preset = 2;  // LATEST, EARLIEST, CUSTOM
  bytes replay_id = 3;             // For resuming from specific point
  int32 num_requested = 4;         // Flow control
  string auth_refresh = 5;         // Internal use
}

// Event response structure
message FetchResponse {
  repeated ConsumerEvent events = 1;     // Batch of events
  bytes latest_replay_id = 2;            // For tracking consumption
  string rpc_id = 3;                     // Request correlation
  int32 pending_num_requested = 4;       // Remaining capacity
}

Official Protocol Buffer Definition

Salesforce provides the official Protocol Buffer definition for the Pub/Sub API in their public repository:

🔗 Official Salesforce Pub/Sub API Proto File

This pubsub_api.proto file contains the complete service definition and message structures:

service PubSub {
  // Bidirectional streaming RPC for event subscription
  rpc Subscribe (stream FetchRequest) returns (stream FetchResponse);

  // Get event schema for dynamic deserialization  
  rpc GetSchema (SchemaRequest) returns (SchemaInfo);

  // Get topic information and permissions
  rpc GetTopic (TopicRequest) returns (TopicInfo);

  // Publish events to topics
  rpc Publish (PublishRequest) returns (PublishResponse);
  rpc PublishStream (stream PublishRequest) returns (stream PublishResponse);
}

Why gRPC Over REST?

FeaturegRPCREST
ProtocolHTTP/2HTTP/1.1
SerializationBinary (Protobuf)JSON/XML
Type SafetyStrong TypingRuntime Validation
Performance7-10x Faster (Public data)Baseline

OAuth 2.0 Client Credentials: Enterprise Security

Why Client Credentials for Pub/Sub API?

The OAuth 2.0 Client Credentials flow is specifically designed for server-to-server authentication scenarios and is recommended by:

- RFC 6749 (OAuth 2.0 specification) for machine-to-machine communication
- Salesforce documentation for Pub/Sub API integrations
- Enterprise security frameworks for automated, non-interactive authentication

sequenceDiagram
    participant App as Spring Boot Application
    participant Auth as Salesforce OAuth
    participant API as Pub/Sub API

    App->>Auth: POST /services/oauth2/token<br/>grant_type=client_credentials
    Auth-->>App: access_token + instance_url

    App->>API: gRPC Subscribe<br/>Headers: accesstoken, instanceurl, tenantid
    API-->>App: Streaming Events

    Note over App : Token auto-refresh every 50 minutes, if configured in connected app

Live Demo

https://github.com/Nagendra080389/salesforce-events-spring

Conclusion

This article explores the Salesforce Pub/Sub API, highlighting its shift from traditional polling methods to a robust, event-driven architecture using gRPC and Protocol Buffers. It covers the core capabilities of the API, including Platform Events, Change Data Capture, and Real-time Event Monitoring, and explains the advantages of using gRPC over REST for high-performance applications. Additionally, it delves into implementing OAuth 2.0 Client Credentials for secure server-to-server authentication, providing a comprehensive Spring Boot integration example to demonstrate real-time data integration.

0
Subscribe to my newsletter

Read articles from Nagendra Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nagendra Singh
Nagendra Singh

Allow me to introduce myself, the Salesforce Technical Architect who's got more game than a seasoned poker player! With a decade of experience under my belt, I've been designing tailor-made solutions that drive business growth like a rocket launching into space. 🚀 When it comes to programming languages like JavaScript and Python, I wield them like a skilled chef with a set of knives, slicing and dicing my way to seamless integrations and robust applications. 🍽️ As a fervent advocate for automation, I've whipped up efficient DevOps pipelines with Jenkins, and even crafted a deployment app using AngularJS that's as sleek as a luxury sports car. 🏎️ Not one to rest on my laurels, I keep my finger on the pulse of industry trends, share my wisdom on technical blogs, and actively participate in the Salesforce Stackexchange community. In fact, this year I've climbed my way to the top 3% of the rankings! 🧗‍♂️ So, here's to me – your humor-loving, ultra-professional Salesforce Technical Architect! 🥳