Integrate with your Backend
The ‘backend connector’ provides the connection between the PowerSync Client SDK and your backend application.
After you’ve instantiated the client-side PowerSync database, you will call connect()
on it, which causes the PowerSync Client SDK to connect to the PowerSync Service for the purpose of syncing data to the client-side SQLite database, and to connect to your backend application as needed, for two purposes:
Purpose | Description |
---|---|
Uploading writes to your backend: | Writes that are made to the client-side SQLite database are uploaded to your backend application, where you control how they’re applied to your backend database (Postgres, MongoDB or MySQL). This is how PowerSync achieves bi-directional syncing of data. |
Authentication integration: | PowerSync uses JWTs for authentication between the Client SDK and PowerSync Service. Your backend application should be able to generate JWTs that the PowerSync Client SDK can retrieve and use for authentication against your PowerSync Service instance. |
Accordingly, you must pass a backend connector as an argument when you call connect()
on the client-side PowerSync database. You must define that backend connector, and it must implement two functions/methods:
Purpose | Function | Description |
---|---|---|
Uploading writes to your backend: | uploadData() | The PowerSync Client SDK automatically calls this function to upload client-side write operations to your backend. Whenever you write to the client-side SQLite database, those writes are also automatically placed into an upload queue by the Client SDK, and the Client SDK processes the entries in the upload queue by calling uploadData() . You should define your uploadData() function to call your backend application API to upload and apply the write operations to your backend database. The Client SDK automatically handles retries in the case of failures. See Writing Client Changes for considerations on the backend implementation. |
Authentication integration: | fetchCredentials() | This is called every couple of minutes and is used to obtain a JWT from your backend. The PowerSync Client SDK uses that JWT to authenticate against the PowerSync Service. See Authentication Setup for instructions on how the JWTs should be generated. |
Example implementation
For an example implementation of a PowerSync ‘backend connector’, see the Getting Started section of the SDK reference for your platform:
Flutter
React Native & Expo
JavaScript Web
Node.js (alpha)
Kotlin Multiplatform
Swift
More Examples
For additional implementation examples, see the Example / Demo Apps section.
Next Step
The next step is implement the necessary server-side functionality in your backend application to handle the above:
App Backend Setup →