Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-Client synchronization #54

Open
kettanaito opened this issue Mar 18, 2021 · 2 comments
Open

Server-Client synchronization #54

kettanaito opened this issue Mar 18, 2021 · 2 comments
Labels
feature New feature or request help wanted Extra attention is needed

Comments

@kettanaito
Copy link
Member

In order to use this library for SSR scenarios, there should be a way to synchronize the DB state on the server based on the operations performed on the client.

Scenario

  1. I load a page with an empty BD on the server.
  2. I load the client, it gets the payload from the server, which gets it from the server-side DB.
  3. I perform some client-side interactions, creating a new entity in the client-side DB.
  4. I reload the page. I should see the client-side DB state being delivered from the server.

This doesn't have to be an actual synchronization, given the limitations of sharing data between environments. It may be a smart client-side DB that hydrates the values from the storage (#49). That would, however, result in the SSR mismatch with the different data between the server and the client.

@kettanaito
Copy link
Member Author

kettanaito commented Mar 22, 2021

Option 1: WebSockets

Spawn a local WebSocket server to signal events from the db usage on the client to the server-side db usage.

Advantages

  • Real-time synchronization using an established protocol.

WebSocket is a great choice, as data updates may be bidirectional:

  • Client updated -> apply to the server.
  • Server update (i.e. Next.js API route) -> apply to the client.

Disadvantages

  • Need to spawn a server, which means having to run an extra command as a part of your work with data.
    • This can be negated, as the server may be spawned internally, without asking the user to execute any extra commands.

It would be great to spawn the WS server implicitly, never exposing it to the end-user. The server address could be mafically shared between the server- and the client-side DB instances.

Implementation

The library determines if the current environment is Node.js, and if so, creates a WebSocket server. The server listens to client-side update events and applies them to the server db instance. The server emits server-side update events to the client as well.

The client-side part of the library attempts to connect to the server. If the connection fails (no server counterpart), handle it gracefully. If the connection is established, the server and the client may be synchronized via socket events.

This was referenced Apr 10, 2021
@kettanaito
Copy link
Member Author

Observation: Next.js re-evaluates the db.js module where you create the database. This means even if the client signals to update the server-side db instance, and it updates, when the page refreshes all the server runtime is thrown away, and the db is instantiated anew.

@kettanaito kettanaito added the feature New feature or request label Apr 16, 2021
@kettanaito kettanaito added the help wanted Extra attention is needed label Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant