0.0.11 • Published 7 years ago

@wessberg/syncdb-server v0.0.11

Weekly downloads
-
License
MIT
Repository
-
Last release
7 years ago

SyncDBServer NPM version

The server part of the offline-first, observable, decoupled, peer-to-peer distributed database SyncDB.

Installation

Simply do: npm install @wessberg/syncdb-server.

What is it

This is the server-part of the Offline-First, observable, decoupled Peer-To-Peer Distributed database SyncDB. It uses a central server to propagate messages around between clients, but it has no centralized data store. This means that the data in the network at all times reflects the contents of the local databases of the peers, and thus provides fully decentralized storage.

Disclaimer

It is still very early days for the synchronization flow between the clients and the server. It doesn't tackle failure resilience or scenarios where a peer simply doesn't reply. For now, use this library with caution.

Reasoning for the central server

  • "Push" requests should be able to go through, even if no other peers exist in the network.
  • A "single source of truth" for providing clients with unique ClientUUIDs was preferable.
  • The central server knows which clients exist in the network rather than having to use WebRTC in the browser, which can be costly and still lacks broad browser support.

Usage

The server sits in the middle and takes care of propagating requests around the peers. You shouldn't need to do anything else than just instantiate it within an environment where it can host a connection on the provided host and port.

Creating a server

If you pass in a TLS certificate and TLS key, the SyncDBServer will be hosted on TLS (as a Secure WebSocket Server). Otherwise, a standard WebSocket server will be hosted.

// Generate a ws:// WebSocket server:
const server = new SyncDBServer({url: "localhost:8000"});
await server.listen();

To make it wss, provide a TLS certificate and TLS key:

// Generate a secure wss:// WebSocket server:
const server = new SyncDBServer({url: "wss://localhost:8000", tlsCert: readFileSync("cert.pem"), tlsKey: readFileSync("key.pem")});
await server.listen();

You can also provide a TLS Chain certificate, if you didn't chain together root certificates in you certificate file:

// Generate a secure wss:// WebSocket server with a chain certificate:
const server = new SyncDBServer({url: "wss://localhost:8000", tlsCert: readFileSync("cert.pem"), tlsKey: readFileSync("key.pem"), tlsChain: readFileSync("chain.pem")});
await server.listen();

Closing a server

If you want to close the server, here's how to do it:

await server.close();

Changelog:

v0.0.11:

  • Made the constructor take a TLS chain as well, if required.

v0.0.10:

  • Made the constructor take a TLS certificate and TLS key if WSS is wanted.

v0.0.9:

  • Added TLS (WSS) support.

v0.0.8:

  • Updated README and bumped dependencies.

v0.0.7:

  • Added support for TLS. Certificates are auto-generated, so you don't need to do anything. TLS is auto-detected from the URL that is passed to the constructor.
  • BREAKING: Constructor now only takes a URL rather than separate 'host' and 'port' keys. The host, port and path are auto-detected from the URL.

v0.0.6:

  • Bumped dependencies.

v0.0.5:

  • Bumped dependencies.

v0.0.4:

  • Refactoring. Bumped dependencies.

v0.0.3:

  • Refactoring and documentation.

v0.0.2:

  • Bumped dependencies and made changes to reflect changes in dependencies.

v0.0.1:

  • First release.