We’re building a Node.js service to stream real-time analytics events from Genesys Cloud to a Kafka topic. The goal is to capture routing.queue.events and routing.agent.events with minimal latency. We’re using the @genesyscloud/purecloud-platform-client-v2 SDK, specifically the PlatformClient and its WebSocket wrapper.
The setup works fine for low-to-medium traffic. We subscribe to the topics, receive the events, and push them to Kafka. But during peak hours, the WebSocket connection starts dropping packets or the process crashes with a FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory.
I’ve traced the issue to the event backlog. When the consumer (our Node app) lags behind the producer (Genesys Cloud), the WebSocket buffer fills up. The SDK doesn’t seem to have a built-in backpressure mechanism that pauses the subscription or drops old events gracefully. It just keeps buffering until OOM.
Here’s the core subscription logic:
import { PlatformClient } from '@genesyscloud/purecloud-platform-client-v2';
const client = PlatformClient.init({
clientId: process.env.GC_CLIENT_ID,
clientSecret: process.env.GC_CLIENT_SECRET,
baseUrl: 'https://api.mypurecloud.com'
});
const subscription = await client.NotificationApi().notificationSubscriptionPost({
subscriptionBody: {
topics: ['routing.queue.events', 'routing.agent.events'],
protocol: 'websocket'
}
});
subscription.on('event', (event: any) => {
// Push to Kafka here
kafkaProducer.send({ topic: 'gc-analytics', messages: [{ value: JSON.stringify(event) }] });
});
I’ve tried increasing the Node.js heap size (--max-old-space-size=4096), but that’s just a band-aid. The real problem is that the SDK keeps accumulating events in memory while Kafka is processing.
Is there a way to configure the WebSocket subscription to discard backlogged events if the consumer is slow? Or should I be implementing a custom WebSocket client with explicit backpressure handling instead of relying on the SDK? The docs are sparse on handling high-throughput scenarios.