sync_client icon indicating copy to clipboard operation
sync_client copied to clipboard

Select DB to be synched by each browser

Open pchainho opened this issue 5 years ago • 10 comments

Hi, is it possible to have multiple browsers each one syncing different DBs in the same sync-server?

For example, having browser A syncing DB X with browser B and browser C syncing DB Y with browser B.

So far I've only managed to have all DBs synched among all browsers connected to the same server

thanks

pchainho avatar Sep 18 '18 02:09 pchainho

No it is currently not possible to have one instance of the sync-server synchronising different DBs. All tables found in the path of the sync-server instance will be synched. If you want to sync different DBs you will need a different sync-server instance for each DB.

nponiros avatar Sep 21 '18 07:09 nponiros

Thanks for your answer @nponiros . Digging a bit into the code I've found a way to pass the table name I want to sync with, in the connection options and then I use it to filter the data retrieved from the server here: https://github.com/nponiros/sync_client/blob/master/src/poll_sync_protocol.js#L34

Of course it would be better to perform the filter at the server side :)

Now I'm looking for a way to sync from a certain sync revision number that I'm also passing in the connection options. Sych revision number would be used here https://github.com/orgs/reTHINK-project/projects/3 in the baseRevision and syncedRevision. Unfortunately It doesn't seem to work.

Let me try to explain the overall scenario. I have a group chat App that is using a msg broker, without any storage capability, to exchange messages and dexie is used to manage the local storage.

In order to enable offline peers receive message when they are back online, one of the peers (primary) is syncing the local storage with the sync-server. ie they would query the sync-server about messages sent when they were offline. When the primary peer goes offline another peer tries to take its role and resume the sync of the local storage with the sync-server using the last synced revision.

Everytime there is a new sync revision (retrieved by the primary peer from appliedRemoteRevision in the _syncNodes table ) it publishes it to all other peers. Such revision number is then used by new primary peers when resuming the sync with the remote sync-server to avoid loading the full data.

However, when connected using such revision number, my local changes are never sent to the remote server. I've tried to increase or decrease the revision but it doesn't work

Is this the expected behavior? Am I missing something?

Thanks a lot for your time

pchainho avatar Oct 02 '18 15:10 pchainho

The baseRevision and syncedRevision are internally used by dexie-syncable to know what changes to send. I’m guessing you would have to adjust it also and not only the sync-client. Sync-client basically just offers a means for the server communication. Dexie-syncable decides what to send to the server given the revision numbers stored internally.

You wrote about the _syncNodes table so maybe you are already changing something in dexie-syncable and I just don’t fully understand the issue yet.

nponiros avatar Oct 03 '18 09:10 nponiros

I didn't touch dexie-syncable perhaps I should also post an issue in the Dexie repo.

pchainho avatar Oct 03 '18 12:10 pchainho

At the end I realized I had something wrong in my test. I'm able to send local changes to sync-server as soon as the sync point I set in the connection options matches the one I see in the sync-server web page:

image

However, this revision number I see in the web page is not incremented with changes sent to sync-server. But checking sync-server responses, the revision number is indeed incremented:

image

If I use this revision number to set the sync point in the connection options I got this error: "Error: Error Can't insert key <KEY>, it violates the unique constraint"

I suspect this error is caused by the mismatch between the revision number I see in the sync-server web page and the one retrieved from the sync-server responses and saved in the _syncNodes table

pchainho avatar Oct 04 '18 14:10 pchainho

It would be interesting to figure out which insert/update triggered the constraint error. Was it your data or a syncNode. I don’t remember when the revisions are increased and who does it. As I said the sync-client itself doesn’t do much. The heavy lifting is done by dexie-syncable and the sync-server. You would probably have to read the dexie-syncable code to know what exactly happens. I might have some time for it in the next couple of days but I can’t promise anything.

nponiros avatar Oct 04 '18 18:10 nponiros

Ok I've tried without the baseRevision, just the syncedRevision and it seems to work, no constraint error.

Now, we trying to use it with WSS, and the sync-server is configured with:

{
  "logging": {
    "errorLogFileName": "error.log",
    "accessLogFileName": "access.log"
  },
  "server": {
    "port": 3000,
    "protocol": "wss",
    "requestSizeLimit": "100kb",
    "wss": {
      "key": "./cert/key_localhost.pem",
      "cert": "./cert/cert_localhost.pem"
    },
    "cors": {}
  },
  "db": {
    "connector": "NeDB",
    "opts": {}
  },
  "sync": {
    "partialsThreshold": 1000
  }
}

However, we see a connection error on the check message sent to https://localhost:3000/check and it seems no sync-server process is handling it. Are we missing anyting?

pchainho avatar Oct 10 '18 15:10 pchainho

I never found the time to implement websockets support for the sync-client. Only the sync-server supports websockets. The /check call is needed only for the poll protocol because you do not have a permantent connection to the server. With websockets you are either connected (you are online) or disconnected (you are offline).

If you use a websocket, the current version of the sync-client won’t add much value. Do feel free to implement websocket support and open a pull request :)

nponiros avatar Oct 14 '18 07:10 nponiros

:) any hint ? Any syncable implementation currently working with sync-server WSS?

pchainho avatar Oct 16 '18 07:10 pchainho

This https://github.com/dfahlander/Dexie.js/tree/master/samples/remote-sync/websocket might be a good start. I’m afraid that I don’t have anything better to offer currently

nponiros avatar Oct 19 '18 16:10 nponiros