Skip to main content

Quick Start

You can install our libraries from npm.

npm install @mycelial/core
npm install @mycelial/websocket

Import our modules.

import * as Mycelial from '@mycelial/core';
import * as Websocket from '@mycelial/websocket';

Create an instance of our library, called a Spore, by calling create and passing it a namespace and a unique client id.

const spore = Mycelial.create('contacts', 123);

Create and attach a network transport

const ws = Websocket.create(spore, {
endpoint: 'wss://v0alpha-relay.fly.dev/v0alpha',
});

Subscribe to changes. update events represent local changes, apply events represent remote changes.

spore.events.addEventListener('update', (evt) => {
console.log('update', evt);
});
spore.events.addEventListener('apply', (evt) => {
console.log('apply', evt);
});

Add a record to the spore.

spore.commit([
{
$id: 100,
name: 'James M.',
phone: '5555555555',
email: '[email protected]',
},
]);

Complete Snippet

import * as Mycelial from '@mycelial/core';
import * as Websocket from '@mycelial/websocket';

const spore = Mycelial.create('contacts', 123);

const ws = Websocket.create(spore, {
endpoint: 'wss://v0alpha-relay.fly.dev/v0alpha',
});

spore.events.addEventListener('update', (evt) => {
console.log('update', evt);
});
spore.events.addEventListener('apply', (evt) => {
console.log('apply', evt);
});

spore.commit([
{
$id: 100,
name: 'James M.',
phone: '5555555555',
email: '[email protected]',
},
]);