Skip to content

phosphorco/livestore-bun-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@phosphor/bun-sync

A Bun-native WebSocket sync backend for LiveStore.

This package provides a high-performance synchronization backend for LiveStore applications running on Bun, leveraging Bun.serve for WebSockets and bun:sqlite for persistence.

Features

  • Bun Native: Built directly on Bun.serve and bun:sqlite for maximum performance.
  • WebSocket Sync: Real-time bidirectional synchronization between clients and server.
  • Persistence: robust event storage using SQLite.
  • LiveStore Compatible: Implements the standard SyncBackend interface.

Installation

bun add @phosphor/bun-sync

Usage

Server

Create a server entry point (e.g., server.ts):

import { startServer } from "@phosphor/bun-sync/server";
import { makeSqliteStorage, setStorage } from "@phosphor/bun-sync/server/storage";
import { Effect } from "effect";

// 1. Initialize Storage
const storage = makeSqliteStorage("sync.db");
// Ensure tables are created
Effect.runPromise(storage.init());

// 2. Inject Storage into Server
setStorage(storage);

// 3. Start Server
const server = startServer(3000);

console.log(`Sync server running at ws://localhost:${server.port}`);

Client

In your LiveStore application (e.g., adapter.ts):

import { makeAdapter } from "@livestore/adapter-web"; // or adapter-node
import { makeBunSyncBackend } from "@phosphor/bun-sync/client";
import { Effect } from "effect";

export const adapter = makeAdapter({
  // ... other adapter options (storage, etc.)
  sync: {
    backend: makeBunSyncBackend("ws://localhost:3000", "my-store-id"),
  },
});

Development

Running the Demo

This package includes a standalone demo to visualize the sync process.

bun run demo

Open http://localhost:3000 to see the demo client.

Running Tests

bun test

License

Dual MIT / APACHE 2.0

About

Native Bun Websocket backend implementation proof of concept

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published