Worker Keepsync Examples
This section shows examples of how to use keepsync in background workers. These examples demonstrate data fetching, processing, and storage patterns for Node.js environments.
API Data Fetching Worker
A complete example of fetching data from an external API and storing it in keepsync:
{{#include ../../../../../../packages/create/templates/worker/instructions/keepsync/examples/worker/index.ts}}
Key Concepts Demonstrated
- Data Fetching: Fetching data from external APIs
- Document Operations: Using
readDoc()
andwriteDoc()
for data storage - Data Merging: Combining existing data with new data
- Error Handling: Proper error handling in worker contexts
- Path-based Storage: Using filesystem-like paths for data organization
Worker Patterns
Workers typically follow these patterns:
- Scheduled Data Fetching: Periodically fetch data from external sources
- Document Storage: Store processed data in keepsync documents
- Data Transformation: Process raw data into structured formats
- Error Recovery: Handle API failures and network issues gracefully
Running the Example
To run this example:
- Create a new Tonk worker:
tonk create
(choose Worker) - Replace the generated code with the example above
- Configure your API endpoints and credentials
- Start the worker:
pnpm dev
The worker will fetch data and store it in keepsync, making it available to all connected applications!