Skip to content

substrate-system/mergeparty

Repository files navigation

Merge Party

tests types module semantic versioning Common Changelog install size license

Automerge + Partykit.

Based on automerge-repo-sync-server.

This creates 1 partykit room per document, using the automerge document ID as the room name.

Contents

Install

npm i -S @substrate-system/mergeparty

Use

Create a backend (the websocket/partykit server) and a browser client.

Backend

Just need to export a class that extends the MergeParty class from this module.

See ./example_backend.

import { MergeParty, CORS } from '@substrate-system/mergeparty/server'

export default class ExampleServer extends MergeParty {
  static async onBeforeConnect (request:Party.Request, _lobby:Party.Lobby) {
    // auth goes here
  }
}

HTTP

Also you can make HTTP calls to the server:

http://localhost:1999/parties/main/<document-id-here>

You should see a response

👍 All good
/health
http://localhost:1999/parties/main/<document-id-here>/health

Response:

{
  "status": "ok",
  "room": "my-document-id",
  "connectedPeers": 0
}

Browser Client

See ./example/ for the browser version.

This is a small wrapper around @automerge/automerge-repo-network-websocket, just adding some parameters for partykit.

export class PartykitNetworkAdapter extends WebSocketClientAdapter {
    constructor (options:{
      host?:string
      room:string
      party?:string
    })

Browser Example

Create a new in-browser automerge node.

import {
    IndexedDBStorageAdapter
} from '@automerge/automerge-repo-storage-indexeddb'
import { PartykitNetworkAdapter } from '@substrate-system/merge-party/client'

const repo = new Repo({
    storage: new IndexedDBStorageAdapter(),
})

const doc = repo.create({ text: '' })
documentId = doc.documentId

// use the document ID as the room name
const networkAdapter = new PartykitNetworkAdapter({
    host: PARTYKIT_HOST,
    room: documentId
})

repo.networkSubsystem.addNetworkAdapter(networkAdapter)
await networkAdapter.whenReady()

// ... use the repo ...

Modules

This exposes ESM and common JS via package.json exports field.

ESM

import { MergeParty } from '@substrate-system/mergeparty'

Common JS

require('@substrate-system/mergeparty')

Develop

start a localhost server

Use vite + local partykit server.

npm start

start partykit

npx partykit dev

About

Automerge + Partykit

Resources

License

Stars

Watchers

Forks

Packages

No packages published