Skip to content

Offline Support for scorm-again ​

This document explains how to use the offline support feature in scorm-again, which allows SCORM modules to function without an active internet connection and sync data back to the LMS when connectivity is restored.

Overview ​

The offline support feature enables:

  1. Running SCORM modules locally on a device without network connectivity
  2. Automatically storing SCORM data locally when offline
  3. Synchronizing data back to the LMS when the network connection is re-established
  4. Ideal for mobile applications, remote areas, or unreliable network conditions

Configuration ​

To enable offline support, you need to set several options when creating your SCORM API:

javascript
// Example configuration
const settings = {
  // Standard settings
  lmsCommitUrl: "https://your-lms.com/api/scorm/commit",
  autocommit: true,

  // Offline support settings
  enableOfflineSupport: true,  // Enable offline support
  courseId: "COURSE-12345",    // Unique identifier for the course
  syncOnInitialize: true,      // Attempt to sync data when initializing API
  syncOnTerminate: true,       // Attempt to sync data when terminating API
  maxSyncAttempts: 5           // Maximum number of attempts to sync an item
};

// Create the API with offline support
const api = new Scorm12API(settings);

Offline Support Settings ​

SettingDefaultDescription
enableOfflineSupportfalseMaster switch to enable/disable offline functionality
courseId""Unique identifier for the course or content module
syncOnInitializetrueWhether to try syncing offline data when the API initializes
syncOnTerminatetrueWhether to try syncing offline data when the API terminates
maxSyncAttempts5Maximum number of sync attempts for each data item

How It Works ​

Automatic Mode Switching ​

The API automatically detects network connectivity status and switches between online and offline modes:

  • When online, data is sent directly to the LMS as usual
  • When offline, each commit is queued in localStorage in the configured dataCommitFormat, with the course id and the learner id
  • If the same course and learner still have unsynced commits when Initialize runs, the API loads them, oldest first, over any data you loaded from the LMS. Only values the SCO can write are restored. LMS-controlled values, such as the learner id, keep the values you loaded, and exit and session_time are not restored. Commits in the params format are not restored.
  • When connection is restored, the commits for the current course and learner are synchronized back to the LMS. The stored copy for a course is removed once all of its commits have synced.
  • Commits queued by older versions have no learner id. They are synced for their course but never restored.

Synchronization Events ​

Each sync run emits OfflineDataSyncing when it starts, then one result event:

  • OfflineDataSynced when the LMS accepted every commit in the run
  • OfflineDataSyncFailed when a commit failed and stays queued, a commit was dropped after maxSyncAttempts, or the run threw an error

This applies to every sync, whether Initialize, Commit, Terminate or a network status change started it. No events fire when a sync is skipped because nothing is queued for the current course and learner, the device is offline, or another sync is already running.

javascript
// Listen for offline data sync events
api.on('OfflineDataSynced', () => {
  console.log('Successfully synchronized offline data to LMS');
});

// Listen for online/offline status changes
window.addEventListener('online', () => {
  console.log('Device is back online, scorm-again will attempt to sync');
});

window.addEventListener('offline', () => {
  console.log('Device is offline, scorm-again will store data locally');
});

Custom Network Status Events ​

For mobile applications and other environments where the browser's navigator.onLine may not accurately reflect network status, scorm-again provides a custom event API to programmatically update network status.

Using the Custom Event ​

The recommended way for mobile apps to communicate network status changes is through the scorm-again:network-status custom event:

javascript
// Notify scorm-again when the device goes online
window.dispatchEvent(new CustomEvent('scorm-again:network-status', {
  detail: { online: true }
}));

// Notify scorm-again when the device goes offline
window.dispatchEvent(new CustomEvent('scorm-again:network-status', {
  detail: { online: false }
}));

Benefits of the Custom Event API ​

  • More Reliable: Mobile network detection APIs are often more accurate than browser navigator.onLine
  • Clean Integration: No need to access internal service properties
  • Future-Proof: Works across all versions and doesn't depend on internal API structure
  • Standards-Based: Uses standard DOM CustomEvent API

Event Details ​

The custom event accepts a detail object with the following property:

PropertyTypeDescription
onlinebooleantrue if device is online, false if offline

When the event is dispatched with online: true, scorm-again will automatically attempt to sync any queued offline data.

Mobile Applications Integration ​

To use offline support in a mobile application:

  1. WebView Integration: Use a WebView to load your SCORM content and scorm-again
  2. Configuration: Set up the API with offline support enabled
  3. Storage Access: Ensure your WebView allows localStorage access
  4. Network Handling: Use the custom event API to communicate network status changes

React Native Example ​

The React Native guide shows the full setup: the shared bridge script, running it at document start, and sending connectivity changes from native code. Its configuration sets lmsCommitUrl, enableOfflineSupport and courseId. Without a commit URL, commits are neither sent nor queued.

Detailed Mobile Framework Examples ​

For more comprehensive examples of implementing offline support in various mobile frameworks, please refer to these detailed guides:

Limitations and Considerations ​

  1. Storage Limits: localStorage typically has a 5-10MB limit, which may affect large courses
  2. Security: Data is stored unencrypted in localStorage by default
  3. Data Persistence: Mobile apps may clear localStorage in certain scenarios
  4. Conflict Resolution: There is no conflict handling. Queued commits are replayed one at a time, in the order they were stored
  5. Connectivity Detection: Some browsers may have delayed connectivity status updates

Best Practices ​

  1. Always provide a meaningful courseId to properly identify offline data
  2. Consider implementing additional local storage mechanisms for very large courses
  3. Test thoroughly in various connectivity scenarios
  4. Implement proper error handling for sync failures
  5. Consider implementing your own encryption for sensitive data

Troubleshooting ​

If you encounter issues with offline support:

  1. Data Not Syncing: Verify network connectivity and check browser console for errors
  2. Storage Errors: Check localStorage limits and browser permissions
  3. Course Not Loading Offline: Ensure all resources are locally available
  4. Mobile Integration Issues: Verify WebView configuration and JavaScript bridging

Released under the MIT License.