LWC Twitter feed component not auto-refreshing on social listening dashboard after initial load

I’ve built a custom LWC component to display Twitter feeds on our Social Listening dashboard, but I’m running into issues with auto-refresh functionality. The component loads initial data fine using an Apex controller that calls the Twitter API, but the feed doesn’t update automatically.

I’ve implemented a setInterval in the connectedCallback lifecycle hook to poll for new tweets every 30 seconds, but after about 5-6 refreshes, the component stops updating and I start seeing API rate limit errors in the console. The Twitter API has a rate limit of 180 requests per 15-minute window, but my calculations suggest we shouldn’t be hitting that threshold.

connectedCallback() {
  this.loadTweets();
  this.refreshInterval = setInterval(() => {
    this.loadTweets();
  }, 30000);
}

The social media team needs real-time updates for brand monitoring, but this is causing data gaps. Has anyone dealt with similar polling challenges in LWC components that interact with external APIs? I’m wondering if there’s a better approach to handle the lifecycle hooks or implement smarter rate limit handling.

Maya’s right about the cleanup, but there’s more to consider here. For external API integrations with rate limits, you should implement exponential backoff and caching strategies. Store the last successful API call timestamp in a component property and only make new calls if enough time has passed. Also, consider using a platform event or streaming API approach instead of polling. Your Apex controller could make scheduled API calls and publish platform events that your LWC subscribes to. This way, multiple users viewing the dashboard don’t each trigger separate API calls. We implemented this for a similar social monitoring use case and reduced API calls by 80% while improving real-time performance.

I’ve seen this pattern before. Your issue is likely that you’re not cleaning up the interval properly when the component unmounts or re-renders. Without cleanup in disconnectedCallback, you’re creating multiple intervals that stack up and hammer the API. Each time the component lifecycle triggers, you’re adding another setInterval without clearing the previous one. Add a disconnectedCallback to clear the interval and you’ll see your rate limit issues disappear.

Good point about the cleanup. I didn’t have a disconnectedCallback at all. But James, I’m interested in your platform event approach. How do you handle the initial load when a user first opens the dashboard? Do they wait for the next scheduled event, or is there a way to get immediate data plus subscribe to updates?

Building on everyone’s suggestions, here’s what I’d recommend as a complete pattern. The lifecycle hook management is critical but often overlooked in LWC development.

Here’s a comprehensive solution addressing all three focus areas: lifecycle hooks, polling strategy, and rate limit handling.

First, fix your lifecycle hook management with proper cleanup:

connectedCallback() {
  this.loadTweets();
  this.startPolling();
}

disconnectedCallback() {
  this.stopPolling();
}

startPolling() {
  this.refreshInterval = setInterval(() => {
    this.checkAndLoadTweets();
  }, 120000); // 2 minutes
}

stopPolling() {
  if (this.refreshInterval) {
    clearInterval(this.refreshInterval);
  }
}

For intelligent rate limit handling, implement a tracking mechanism:

checkAndLoadTweets() {
  const now = Date.now();
  if (this.rateLimitResetTime && now < this.rateLimitResetTime) {
    this.showRateLimitMessage();
    return;
  }
  this.loadTweets();
}

In your Apex controller, capture rate limit headers:

HttpResponse res = http.send(req);
if (res.getStatusCode() == 429) {
  String resetTime = res.getHeader('X-Rate-Limit-Reset');
  // Return reset timestamp to LWC
}

For optimal performance, combine this with the platform event pattern James suggested. Create a scheduled Apex job that runs every 2 minutes to fetch tweets once and publish a platform event. Your LWC subscribes to this event using the lightning/empApi module. This architecture ensures:

  1. Lifecycle hooks are properly managed with cleanup preventing interval stacking
  2. setInterval polling is used efficiently with longer intervals (2+ minutes) as a fallback
  3. API rate limit handling includes reset time tracking, user notifications, and the primary pattern of centralized API calls via platform events

The platform event approach is crucial because if 20 sales reps have the dashboard open, you’re making 1 API call every 2 minutes instead of 20. This keeps you well under Twitter’s 180 requests per 15-minute window (that’s 12 requests per 15 minutes vs. potentially 150+).

Implement a manual refresh button as Priya suggested for user control. Add a loading spinner during API calls and a timestamp showing ‘Last updated: X minutes ago’ so users have visibility into data freshness. Store the last successful API response in sessionStorage as a cache layer, so if the component remounts during the same session, it can display cached data immediately while fetching fresh updates in the background.

This pattern has worked reliably for social listening dashboards handling multiple external APIs with various rate limiting schemes.

I’d add that you should implement proper error handling for rate limit scenarios. When you hit a 429 response from Twitter, extract the X-Rate-Limit-Reset header which tells you exactly when your rate limit window resets. Store this timestamp and pause all API calls until that time. Your LWC can display a friendly message like ‘Updates paused due to API limits. Resuming at HH:MM.’ This prevents wasted API calls and gives users transparency. Also consider implementing a manual refresh button so users can control when they want to fetch new data if auto-refresh is paused.