• Stars
    star
    397
  • Rank 104,756 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Makes it easy to write offline-first applications with realtime syncing and server side persistence.

SyncedDB

Build Status

SyncedDB makes it easy to write offline-first applications with real-time syncing and server-side persistence.

SyncedDB makes web applications work beautifully both online and offline.

You can write your client as if everything was stored offline! SyncedDB takes care of synchronizing the local database to other clients in real time.

Table of contents

Why

Since the widestream adoption of IndexedDB writing web applications with full offline support has been viable. But when storing data offline web applications typically lack the ability to seamlessly make a user's data available across devices. SyncedDB is a library that gives web applications the best of both worlds: a fully functional offline experience with real-time or on-demand synchronization of data when online.

What

SyncedDB was built with the following design goal: Be as simple as possible while still providing all the features and flexibility necessary to easily create efficient and secure real-time synchronizing web applications that works offline.

SyncedDB is a lightweight layer on top of IndexedDB. It strips away all the boilerplate that the IndexedDB API requires by introducing implicit transactions, convenience methods and promises for all asynchronous operations.

Server side SyncedDB stores a list of changes that clients can request/subscribe and post/publish to. The SyncedDB client communicates with the backend through WebSockets to achieve synchronization in real time. Furthermore, the client provides elegant conflict handling and events for reacting to changes published from the server.

Example

Client

var stores = {
  tasks: [ // One store named 'tasks'.
    ['byCreation', 'createdAt'] // With one index into the 'createdAt' property.
  ]
};

var db = syncedDB.open({ // Open database.
  name: 'todoApp',
  version: 1,
  stores: stores,
  remote: 'localhost:8080',
});

db.tasks.put({ // Add one task to database.
  description: 'Task description',
  finished: false,
  createdAt: Date.now()
});

db.tasks.byCreation.getAll() // Get all task elements sorted after creation and output them on the console.
.then(function(tasks) {
  tasks.forEach(console.log);
});

db.tasks.on('add', function(e) { // Add a handler if a new task element is pushed from remote.
  console.log("New task from server: ", e.record); // Handle task by printing it on the console.
});

// Start syncing continuously, the server will now.
// push and pull changes in real time.
db.sync('tasks', {continuously: true});

Server

var Server = require('synceddb-server');

// Persistence within memory (you can use other adaptors for PostgreSQL, MySQL & CouchDB.
var sdbPersistence = require('synceddb-persistence-memory');

var server = new Server({
  port: 8080,
  store: sdbPersistence.create(),
});

Run the example by starting the server with node and run the client in a browser incognito mode. Then restart the client and watch the console output.

See a more sophisticated version of this example here

Main features

  • No additional abstractions on top of IndexedDB. It exposes the same raw power and performance but through a significantly more convenient API
  • Compact declarative store and index definitions with automatic upgrades
  • Uses promises for all async operations β€” even inside IndexedDB transactions
  • Synchronizes data through WebSockets and sends only compact diffs down the wire. This makes the network usage light and efficient.
  • Makes it easy and intuitive to handle conflicts.
  • Simple and highly flexible backend. Bring your own server-side validations, authentication, authorization, etc. Plug in any database you like and store data any way you want alongside the format that SyncedDB uses internally.

How is it different

Some libraries cater to multiple storage backends and thus end up with a limited feature set following the lowest common denominator. Others implement a new database on top of the browser's native storage facilities. This highly increases complexity and reduces performance. By being a small wrapper around IndexedDB, SyncedDB gains some of its key features: simplicity, power and performance.

The SyncedDB backend was designed to be as flexible as possible. Users can easily plug in any database they want, create custom message handlers at relevant points and extend the communication between the client and the server with custom messages.

State

SyncedDB is still under development. Expect rough edges.

Storage options

Persistence options are provided based on the following currently supported databases:

  • In memory (for developing)
  • MySQL
  • PostgreSQL
  • CouchDB

SyncedDB makes it easy to use different server side persistence strategies. These are easy to write (take a look at the existing options) and a test suite is provided.

Todo

  • API for IndexedDB cursors
  • Handle terminated connections with the server
  • Add more documentation and additional examples

Examples

  • Todo app. Demonstrates the basics of how to use SyncedDB both client side and sever side.
  • Authentication. Shows how the protocol between the server and the client can be extended to facilitate authentication.
  • Counters app. Showcases a potential conflict handling strategy where numeric changes are treated as differences/deltas.

Documentation

More Repositories

1

flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
JavaScript
1,566
star
2

functional-frontend-architecture

A functional frontend framework.
JavaScript
1,444
star
3

union-type

A small JavaScript library for defining and using union types.
JavaScript
475
star
4

dffptch

A micro library for diffing and patching JSON objects using a compact diff format
JavaScript
170
star
5

composable.el

Composable text editing for Emacs.
Emacs Lisp
115
star
6

functionize

A library which aids in making any JavaScript library more functional.
JavaScript
49
star
7

projectdo

Context-aware single-letter project commands to speed up your terminal workflow.
Shell
47
star
8

Kran

An entity system written in JavaScript.
JavaScript
42
star
9

Gtk98Icons

An icon theme for GTK that looks like Windows 98
PHP
40
star
10

smart-comment

Smarter commenting for Emacs
Emacs Lisp
40
star
11

sync-promise

Compact synchronized promise implementation. Promises/A+ incompliant. Works inside IdexedDB transactions.
JavaScript
32
star
12

dot-compose

Function composition with dot as a composition operator.
JavaScript
23
star
13

seamless-fantasy

Make fantasy land seamlessly compatible with plain JavaScript data structures.
JavaScript
10
star
14

list-difference

Fast algorithm for finding edits between lists.
JavaScript
10
star
15

duck

πŸ¦† Turns a TypeScript file into JSON describing the files exports.
TypeScript
8
star
16

find-the-function

A tiny tool for finding functions from libraries
TypeScript
7
star
17

ryter

A tiny JavaScript router
JavaScript
7
star
18

web-swipe-view

Horizontal swipe views for mobile web applications
JavaScript
7
star
19

finger-tree

Highly optimized implementation of finger trees in JavaScript
TypeScript
6
star
20

flyview

Efficient views powered by streams/ovservables/functional reactive properties.
JavaScript
6
star
21

fake-raf

A fake requestAnimationFrame perfect for unit testing.
JavaScript
5
star
22

flyd-forwardto

Create a new stream that passes all values through a function and forwards them to a target stream.
JavaScript
4
star
23

maxima-calculus2

Maxima funktioner til lΓΈsning af eksamensopgaver i kurset Calculus 2 pΓ₯ Aarhus Universitet
4
star
24

turing-patterns

Multi-Scale Turing Patterns
JavaScript
4
star
25

reflex-examples

A collection of examples using Reflex.
Haskell
3
star
26

flyd-scanmerge

Flyd module for conveniently merging and reducing several streams into one.
JavaScript
3
star
27

flyd-obj

Functions for working with stream in objects.
JavaScript
3
star
28

category-theory-notes

TeX
3
star
29

matmod

Handy functions for matMod written in R for Jupyter
Jupyter Notebook
3
star
30

dnd-scroll

Proper edge scroll when dragging with HTML 5 drag and drop!
JavaScript
3
star
31

dffptch-haskell

A small library for diffing and patching JSON objects using a compact diff format
Haskell
2
star
32

keyano-vscode

Next-generation keyboard-driven editing language. Edit code at the speed of light.
TypeScript
2
star
33

flyd-filter

Filter function for Flyd.
JavaScript
2
star
34

planetsimulator

A physical simulation of planetary motion written in JavaScript
JavaScript
2
star
35

flyd-every

Takes a time interval t and creates a stream of the current time updated every t.
JavaScript
1
star
36

paldepind.github.io

CSS
1
star
37

hareactive-old

Experimental WIP.
JavaScript
1
star
38

vdom-benchmark-snabbdom

Virtual DOM Benchmark implementation for Snabbdom library.
JavaScript
1
star
39

simple-frp

An attempt at creating a very simple FRP library for educational purposes.
TypeScript
1
star
40

flyd-sampleon

sampleOn for Flyd.
JavaScript
1
star
41

software-foundations

My solutions to exercises in Benjamin C. Pierce's Software Foundations
HTML
1
star
42

flyd-keepwhen

keepWhen function for Flyd.
JavaScript
1
star
43

flyd-lift

Lift function for Flyd.
JavaScript
1
star
44

dotfiles

Repository containing my dotfiles.
Emacs Lisp
1
star
45

react-native-chainable-stylesheet

TypeScript
1
star
46

flyd-aftersilence

Flyd module that buffers values from a stream and emits them after a specified duration of silience.
JavaScript
1
star
47

domain-theory

CSS
1
star