powersync-js icon indicating copy to clipboard operation
powersync-js copied to clipboard

Switching Sync Params

Open KarimTamani opened this issue 4 months ago • 11 comments

useEffect(() => {
  (async () => {
    await powerSync.connect(connector, {
      params: { projectId },
    });
  })();
}, [projectId]);

Description I’m building a project where I use PowerSync to sync a local SQLite database with Postgres via a custom backend connector.

I need to load bucket data based on a projectId attribute, so the idea is that instead of syncing all projects, I only sync the currently opened project.

Right now, every time the user switches projects, I call powerSync.connect(...) again with a new params. However, this closes the old WebSocket connection and creates a new one, which introduces a noticeable delay before data is available.

Problem Frequent connect calls just to change parameters aren’t efficient.

Switching projects feels slow because of the teardown + reconnect.

Question / Feature Request Is there an alternative way to update connection params dynamically (e.g., something like a setParams API) without forcing a full reconnect?.

Thank you !

KarimTamani avatar Aug 21 '25 18:08 KarimTamani

We experience the same problem, AFAIK currently there isn't a way to do this but it should be possible in the near feature with sync streams that should act as an alternative to client params.

Related sources: https://roadmap.powersync.com/c/129-sync-streams https://github.com/powersync-ja/powersync-sqlite-core/pull/112

whygee-dev avatar Aug 21 '25 18:08 whygee-dev

It's worth noting that adding new subscriptions with sync streams will also involve a reconnect (at least initially), but due to caching it should generally be much faster when switching between parameters.

simolus3 avatar Aug 21 '25 19:08 simolus3

@KarimTamani @whygee-dev How big are the delays you're typically seeing when connecting with new parameters?

Ideally, reconnecting should be just as fast as a normal http request would have been for the same amount of data. In practice, we do compute some state at the start of a connection, that could slow things down. And websockets connections always have two round-trips minimum, in addition to the establishing the HTTPS connection itself. Re-using the same websocket connection could help address some of that overhead.

rkistner avatar Aug 29 '25 13:08 rkistner

@KarimTamani @whygee-dev we're getting close to an early release of sync streams which have been mentioned above, which could speed up switching between params and in general should be simpler to use than client parameters. Are you interested in testing it as soon as it's available? If so, please let me know if you're on web or React Native.

benitav avatar Sep 01 '25 12:09 benitav

@rkistner I’m experiencing a delay of 1–5 seconds on localhost, and on remote it can reach up to 10 seconds.

The second issue is handling the loading state. Specifically, how can I know when the PowerSync client is connected and has finished loading all data from the bucket?

I tried this:

const { connected, connecting } = powerSync.currentStatus;

But the connected and connecting states don’t indicate whether all the data is fully loaded and ready to use.

KarimTamani avatar Sep 01 '25 13:09 KarimTamani

Thanks a lot! @benitav Currently, I found a temporary solution by loading all user projects on sign-in. It’s not a good practice, but it works for now.

In the meantime, yes , I’d love to test sync streams. I’m on the web using React.

KarimTamani avatar Sep 01 '25 13:09 KarimTamani

We should look into those connect times, more than a second is really quite a lot. Is that the time until connected starts being true? Sync streams work by dynamically reconnecting behind the scenes when you're subscribing to a new stream, so with those times they also wouldn't work that well.

Specifically, how can I know when the PowerSync client is connected and has finished loading all data from the bucket?

If you want to know whether the initial sync has completed, you can use waitForFirstSync() or hasSynced on currentStatus. If you also want to display a loading state while the client is currently busy downloading newer data, syncStatus.dataFlowStatus.downloading should provide that information.

simolus3 avatar Sep 01 '25 14:09 simolus3

@KarimTamani @whygee-dev How big are the delays you're typically seeing when connecting with new parameters?

Ideally, reconnecting should be just as fast as a normal http request would have been for the same amount of data. In practice, we do compute some state at the start of a connection, that could slow things down. And websockets connections always have two round-trips minimum, in addition to the establishing the HTTPS connection itself. Re-using the same websocket connection could help address some of that overhead.

Sometimes it takes as much as 5-10 seconds, other times a couple of seconds.

I tried this:

const { connected, connecting } = powerSync.currentStatus;

But the connected and connecting states don’t indicate whether all the data is fully loaded and ready to use.

You can listen on the status sync / download progress https://docs.powersync.com/client-sdk-references/javascript-web/usage-examples#report-sync-download-progress

Are you interested in testing it as soon as it's available? If so, please let me know if you're on web or React Native.

@benitav Sure ! I'm on web React

whygee-dev avatar Sep 01 '25 14:09 whygee-dev

We should look into those connect times, more than a second is really quite a lot. Is that the time until connected starts being true? Sync streams work by dynamically reconnecting behind the scenes when you're subscribing to a new stream, so with those times they also wouldn't work that well.

Yes, I measured the time between when the listener reports a false connected to when it starts emitting connected true

whygee-dev avatar Sep 01 '25 14:09 whygee-dev

@KarimTamani @whygee-dev Sync streams are now available in an early alpha release for React - we'd love your feedback! please join the discussion here: https://discord.com/channels/1138230179878154300/1422138173907144724/1422138769703829596

benitav avatar Sep 29 '25 11:09 benitav

Hey @benitav , I'm Yahya in discord, I'll try it out ASAP

whygee-dev avatar Sep 29 '25 11:09 whygee-dev