The application provides real-time streaming of Salesforce Change Data Capture (CDC) and Platform Events using Node.js and WebSocket technology. It allows users to:
- Authenticate with Salesforce using OAuth2
- Subscribe to CDC or Platform Events
- View real-time event updates with historical data
- Maintain persistent connection for live updates
graph TD
subgraph Client
UI[Web Browser]
WS_Client[WebSocket Client]
end
subgraph Server
Express[Express Server]
WS_Server[WebSocket Server]
SF_Service[Salesforce Service]
Auth[Auth Controller]
Stream[Stream Controller]
end
subgraph Salesforce
SF_Auth[OAuth2 Endpoint]
SF_Stream[Streaming API]
SF_CDC[CDC Events]
SF_Platform[Platform Events]
end
UI --> Express
WS_Client <--> WS_Server
Express --> Auth
Express --> Stream
Auth <--> SF_Auth
Stream --> SF_Service
SF_Service <--> SF_Stream
SF_Stream --> SF_CDC
SF_Stream --> SF_Platform
WS_Server --> UI
- Backend: Node.js v16+
- Framework: Express.js v4.18
- Template Engine: EJS v3.1
- WebSocket: ws v8.13
- Salesforce Connection: JSForce v1.11
- Session Management: express-session v1.17
- Environment Variables: dotenv v16.0
{
"dependencies": {
"dotenv": "^16.0.3",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-session": "^1.17.3",
"jsforce": "^1.11.1",
"ws": "^8.13.0"
}
}├── .env
├── app.js
├── package.json
├── public/
│ └── css/
│ └── style.css
├── routes/
│ ├── auth.js
│ └── stream.js
├── controllers/
│ ├── authController.js
│ └── streamController.js
├── views/
│ ├── login.ejs
│ └── stream.ejs
└── services/
└── salesforceService.js
app.js
- Entry point for the application
- Express and WebSocket server configuration
- Middleware setup
- Route registration
- WebSocket connection management
routes/auth.js
- OAuth2 authentication routes
- Login endpoint
- Callback handling
- Session management
routes/stream.js
- Streaming subscription routes
- Event handling endpoints
- WebSocket event broadcasting
controllers/authController.js
- Salesforce OAuth2 flow implementation
- Token management
- User session handling
controllers/streamController.js
- CDC and Platform Event subscription logic
- Event data processing
- WebSocket broadcast management
services/salesforceService.js
- Salesforce API integration
- Event subscription management
- Data transformation
- Error handling
views/login.ejs
- Login page UI
- Salesforce authentication button
- Initial user interface
views/stream.ejs
- Main streaming interface
- Event type selection
- Event display container
- WebSocket client implementation
public/css/style.css
- Application styling
- Event container design
- Animation definitions
- Responsive layout
- User clicks "Login with Salesforce"
- System redirects to Salesforce OAuth2 endpoint
- User authenticates with Salesforce
- Salesforce redirects to callback URL
- System stores access token in session
- User is redirected to streaming page
- User selects event type and name
- Client establishes WebSocket connection
- Server subscribes to Salesforce topic
- Salesforce sends events (historical + real-time)
- Server broadcasts events to all connected clients
- Clients display events with appropriate styling
sequenceDiagram
participant Client
participant Server
participant WebSocket
participant Salesforce
Client->>Server: Subscribe to events
Server->>Salesforce: Create streaming topic subscription
Client->>WebSocket: Establish connection
Salesforce->>Server: Send historical events
Server->>WebSocket: Broadcast events
WebSocket->>Client: Display events
loop Real-time Updates
Salesforce->>Server: New event occurs
Server->>WebSocket: Broadcast new event
WebSocket->>Client: Update UI
end
SF_CLIENT_ID=your_client_id
SF_CLIENT_SECRET=your_client_secret
SF_REDIRECT_URI=http://localhost:3000/auth/callback
SF_LOGIN_URL=https://login.salesforce.com- Create Connected App in Salesforce
- Configure OAuth settings
- Enable relevant CDC or Platform Events
- Set appropriate permissions
- Maximum 5 reconnection attempts
- 3-second delay between attempts
- Automatic cleanup of dead connections
-
Authentication Errors
- Invalid credentials
- Token expiration
- Permission issues
-
Subscription Errors
- Invalid event type
- Topic not found
- Permission denied
-
Connection Errors
- WebSocket disconnection
- Network issues
- Server timeout
- OAuth2 secure flow
- Session-based authentication
- HTTPS recommended for production
- No storage of Salesforce credentials
- Session-only token storage
- Secure WebSocket connection
- Connection pooling
- Event buffering
- Client-side throttling
- Asynchronous event handling
- Batch processing for historical events
- Memory management for large event volumes
- Node.js v16+
- 512MB RAM minimum
- Network access to Salesforce APIs
- Modern web browser with WebSocket support
- JavaScript enabled
- Stable internet connection
- Authentication Flow
- WebSocket Connection
- Event Subscription
- Data Display
- Error Handling
- Reconnection Logic
- Unit Tests
- Integration Tests
- End-to-End Tests
- Performance Tests
- WebSocket connection status
- Event processing time
- Error rates
- Memory usage
- Connection count
- Authentication attempts
- Connection events
- Subscription status
- Error details
- Performance metrics