Code Monkey home page Code Monkey logo

todo-replicache-sveltekit's Introduction

todo-replicache-sveltekit

Multiplayer, offline-compatible TODO-MVC Demo. sveltekit-replicache-demo

Play with this app on render

This repository contains sample code for Replicache. The example uses SvelteKit. The backend demonstrates implementations of push, pull, poke, createSpace, and spaceExists handlers, which are required for the Replicache sync protocol.

This is a port of todo-wc, with a few minor additions. The frontend was ported from svelte-todomvc. It uses svelte-sse for server-sent events and in-memory Postgres as the database (copied directly from todo-wc).

My goal in creating this was to learn more about both Replicache and SvelteKit, as porting a codebase forces one to understand it :). At the same time, I wanted to create something useful for others since the official examples on the Replicache website don't include a SvelteKit version. I chose the example with 'Per-Space Versioning' because it was the simplest one that also seemed practical (I couldn't think of a use case where global versioning would be desirable). Read more about it in this blog post.

0. What's Being Demonstrated Here?

This looks and feels like the normal TODO MVC app but has the following additional properties:

  1. It automatically syncs across tabs (even while offline).
  2. It syncs across browsers and devices when online, including when going back online after being offline.
  3. Despite writing every update to the backend, the user experience is snappy regardless of network speed and even when offline (thanks to Replicache's optimistic updates and its use of IndexedDB as a storage engine).
  4. It has all the other properties of an app that uses Replicache, such as the ability to handle conflicts, rebasing, rollbacks, schema changes, etc.
  5. The "syncing unit" here is a "space" (in our case, a list) - you'll see the spaceID in the URL. You can create multiple spaces, but when visiting one, only data related to it is synced back and forth with the backend. This means permissions can be granted per space. Every time you navigate to the root URL or try to go to a non-existing space, a new space is created (you have to be online for space creation to work). You can then copy the URL to another tab or window to see the space staying in sync.

Minor Functionality Additions By Me (over the official example)

  1. Added a primitive "sync pending/all changes saved" marker that's interesting to look at while throttling the network and when going into and out of offline mode.
  2. Added a service worker (the one from the SvelteKit docs) to allow refreshing or navigating to an existing space in offline mode.

Stuff I'd Like to Explore/Add But Haven't Yet

  1. Undo/redo and version history (maybe using Dolt).
  2. A conventional DB schema (e.g a table for items).
  3. Space creation while offline.

Other Thoughts

Notice that Replicache effectively plays the role a Svelte store would and has a very similar API from the frontend's point of view. Since Replicache holds all the data in memory (in addition to persisting it via IndexedDB), the app uses it directly without copying it into a Svelte store or state.

1. Setup

Get Your Replicache License Key (it's free)

$ npx replicache get-license

Set Your VITE_REPLICACHE_LICENSE_KEY Environment Variable, Either in .env or in Your Terminal:

$ export VITE_REPLICACHE_LICENSE_KEY="<your license key>"

Install

$ npm install

2. Start the Dev Server

$ npm run dev

todo-replicache-sveltekit's People

Contributors

isaachagoel avatar

Stargazers

Jeff A. avatar sanghun lee avatar Thomas Brodusch avatar Keith Hall avatar Salem Cobalt avatar  avatar Andrew avatar Lorena Sabino Guedes avatar Ali Amani avatar Tony avatar Wascar J Gonzalez avatar Don Whittaker avatar Nicholas Melton avatar Khalil Charfi avatar Ryan Fraser avatar Jim Law avatar Alex Lévy avatar

Watchers

 avatar

todo-replicache-sveltekit's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.