docs-src/docs/replication-google-drive.md
import {Steps} from '@site/src/components/steps'; import {BetaBlock} from '@site/src/components/beta-block'; import {HeadlineWithIcon} from '@site/src/components/headline-with-icon';
The replication-google-drive plugin allows you to replicate your client-side RxDB database to a folder in the user's Google Drive. This enables cross-device sync for single users without requiring any backend server.
The replication uses the Google Drive API v3 and v2.
You need to enable the Google Drive API in the Google Cloud Console and create credentials (OAuth 2.0 Client ID) for your application.
Your application must handle the OAuth flow to get an accessToken from Google. You can use libraries like @react-oauth/google or the Google Identity Services SDK.
Once you have the accessToken, you can start the replication.
import { replicateGoogleDrive } from 'rxdb/plugins/replication-google-drive';
const replicationState = await replicateGoogleDrive({
replicationIdentifier: 'my-app-drive-sync',
collection: myRxCollection, // RxCollection
googleDrive: {
oauthClientId: 'YOUR_GOOGLE_CLIENT_ID',
authToken: 'USER_ACCESS_TOKEN',
folderPath: 'my-app-data/user-1'
},
live: true,
pull: {
batchSize: 60,
modifier: doc => doc // (optional) modify invalid data
},
push: {
batchSize: 60,
modifier: doc => doc // (optional) modify before sending
}
});
// Observe replication states
replicationState.error$.subscribe(err => {
console.error('Replication error:', err);
});
replicationState.awaitInitialReplication().then(() => {
console.log('Initial replication done');
});
Google Drive does not provide real-time events for file changes. If a user changes data on User Device A, User Device B would not know about it until it periodically polls the Drive API. To achieve real-time updates, this plugin uses WebRTC to signal changes between connected devices.
signaling subfolder on Google Drive.WebRTC is native in browsers but requires a polyfill in Node.js. Use createSimplePeerWrtc() to wrap the polyfill for compatibility with simple-peer:
import nodeDatachannelPolyfill from 'node-datachannel/polyfill';
import { createSimplePeerWrtc } from 'rxdb/plugins/replication-webrtc';
// ...
const replicationState = await replicateGoogleDrive({
// ...
signalingOptions: {
wrtc: createSimplePeerWrtc(nodeDatachannelPolyfill)
}
});
string: The OAuth 2.0 Client ID of your application.string: The valid access token associated with the user.string: The path to the folder in Google Drive where data should be stored.
drive space it must not be the root folder.appDataFolder space it is optional and interpreted relative to the appDataFolder root.docs (for data) and signaling (for WebRTC).'drive' | 'appDataFolder' (optional): Defaults to 'drive'. See the section below.string (optional): Defaults to https://www.googleapis.com. Useful for mocking or proxies.number (optional): Default 10000 (10s). The plugin uses a transaction file in Drive to ensure data integrity during writes. This is the timeout after which a lock is considered stale.By default the plugin stores data in the user visible "My Drive". Set space: 'appDataFolder' to store data in Google Drive's hidden, per-application data folder instead. This is useful on Android and other clients where you want app state synced across the user's devices without cluttering their Drive UI.
const replicationState = await replicateGoogleDrive({
replicationIdentifier: 'my-app-drive-sync',
collection: myRxCollection,
googleDrive: {
oauthClientId: 'YOUR_GOOGLE_CLIENT_ID',
authToken: 'USER_ACCESS_TOKEN',
space: 'appDataFolder'
// folderPath is optional here
},
live: true,
pull: {},
push: {}
});
When using appDataFolder:
https://www.googleapis.com/auth/drive.appdata OAuth scope when you authenticate the user. The regular Drive scopes do not grant access to this space.folderPath is optional. If omitted, the docs and signaling subfolders are created directly in the appDataFolder root.Controls whether binary attachment data is replicated along with documents.
attachments: {} defined.attachments: false to disable attachment replication (only document fields are synced).const replicationState = await replicateGoogleDrive({
// ...
attachments: false, // disable attachment replication
pull: {},
push: {}
});
When attachment replication is enabled, attachment binary data is encoded as base64 and stored in a separate _attachments_data field inside the document's JSON file on Drive. The standard _attachments field only contains metadata stubs (digest, length, type). On pull, the base64 data is decoded back to Blob instances and written to local storage.
Standard RxDB Replication Options for batch size, modifiers, etc.
docs subfolder.[primaryKey].json.modifiedTime of files in Google Drive.transaction file is locked by another device, the write retries until the lock is released or times out.For testing, it is recommended to use google-drive-mock. It simulates the Google Drive API so you can run tests without real credentials.