Workers
Background scripts that run continuously on TelemetryOS devices
What are Workers?
Workers are background scripts that run continuously on TelemetryOS devices, even when your application isn't currently visible in the playlist. They enable background processing, data synchronization, and scheduled tasks without requiring a visible UI.
Workers handle tasks like:
- Fetching data from external APIs
- Synchronizing with inventory or CMS systems
- Monitoring device health
- Caching content for offline use
- Background analytics collection
- Scheduled data updates
How Workers Work
Workers are defined in your telemetry.config.json file and start when the playlist containing your application is loaded. They continue running in the background, even when the playlist moves to other pages.
{
"name": "my-app",
"version": "1.0.0",
"mountPoints": {
"render": "/render"
},
"workers": [
{
"script": "./workers/sync.js"
}
]
}Example Worker
workers/sync.js:
import { configure, store } from '@telemetryos/sdk';
configure('my-app');
async function syncData() {
try {
// Fetch fresh data from external API
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = await response.json();
// Store for application to use
await store().application.set('latest-data', data);
console.log('Data synced successfully');
} catch (error) {
console.error('Sync failed:', error);
}
// Schedule next sync
setTimeout(syncData, 60000); // Every minute
}
// Start sync loop
syncData();Worker Context
- Runs on: Physical devices only
- Lifecycle: Starts when playlist is loaded, continues running in background
- SDK Access: Full SDK API available (storage, media, playlist, etc.)
- No DOM access: Workers are JavaScript contexts without browser DOM
Communication with Render
Workers communicate with your render view through the Storage API:
// Worker writes data
await store().application.set('weather-data', weatherInfo);
// Render subscribes to changes
store().application.subscribe('weather-data', (data) => {
updateDisplay(data);
});Best Practices
- Error handling - Workers must be resilient to failures and network issues
- Periodic execution - Use
setTimeoutorsetIntervalfor scheduling - Resource management - Be mindful of CPU and memory usage on devices
- Graceful degradation - Applications should work if worker fails
- Logging - Log important events for debugging in production
Learn More
For complete details on workers, including lifecycle, configuration, and advanced patterns, see the Mount Points documentation.
Updated 21 days ago