DEV Community

Alex
Alex

Posted on

πŸš€ Introducing react-native-sync-tasks: Native JSI-Powered HTTP Polling Off the JS Thread

Demo

Have you ever needed to periodically fetch data from a remote API in your React Native app? Maybe you're building a chat client that polls for new messages, or a dashboard that refreshes frequently with updated metrics?

Most developers reach for this kind of code:

useEffect(() => {
  const interval = setInterval(async () => {
    const response = await fetch("https://your-api.com/status");
    const data = await response.json();
    // do something
  }, 1000);

  return () => clearInterval(interval);
}, []);
Enter fullscreen mode Exit fullscreen mode

This works... until it doesn’t:

  • Multiple polling tasks run in parallel and get messy ☠️
  • You start hitting performance issues and blocking the JS thread 🚨
  • Managing lifecycles and cleanup becomes tedious 🧠
  • You process the same data repeatedly, wasting CPU cycles πŸ“‰

That’s why I built react-native-sync-tasks β€” a blazing-fast, native JSI-based polling library for React Native, written in C++ and Rust, with a clean and intuitive JS API. It runs outside of the JS thread, leveraging native threads for maximum performance.


🧠 Why use SyncTasksManager?

βœ… Executes periodic polling in native threads, not in JS timers
βœ… Callback only fires when the response has changed (using response hash)
βœ… Centralized task management with start/stop control
βœ… Keeps your JS thread free and UI smooth
βœ… Zero runtime dependencies β€” fully native via JSI


✨ Features

  • πŸ” Periodic HTTP polling with configurable interval
  • πŸ“‘ onData callback when data is received (only if changed)
  • ❌ onError callback for failed requests
  • 🧡 Native execution via C++/Rust threads (JSI)
  • 🧠 Smart deduplication (response body hash)
  • βœ… Centralized control over all polling tasks

πŸš€ Quick Example

import { createTask, SyncTasksManager } from 'react-native-sync-tasks';

const task = createTask({
  config: {
    url: 'https://jsonplaceholder.typicode.com/posts/1',
    interval: 2000,
  },
  onData: (data) => console.log('DATA:', data),
  onError: (err) => console.error('ERROR:', err),
});

SyncTasksManager.addTask(task);
SyncTasksManager.startAll();
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Installation

npm install react-native-sync-tasks
Enter fullscreen mode Exit fullscreen mode

Don’t forget to run pod install on iOS.


βš™οΈ Under the Hood

This library is built entirely with native performance in mind:

  • πŸ¦€ Rust handles the HTTP logic and hashing
  • βš™οΈ C++ bridges with JSI in React Native
  • 🚫 No timers or polling in JS β€” it’s all native

⚠️ Not a true background task

This library does not continue polling when the app is in background or terminated. It is meant for offloading polling work to native threads while the app is active.


πŸ” Use cases

  • πŸ”„ Real-time metrics dashboards
  • πŸ’¬ Chat apps polling for new messages
  • πŸ“² Device status updates
  • πŸ”” Periodic checks to backend APIs

πŸ”— Links


If you’re building something that needs smooth, high-frequency API polling without hurting performance β€” give it a try!

Feedback and contributions are welcome πŸ™Œ

Top comments (0)