Skip to main content

Quick Start

You can install our libraries from npm.

npm install @mycelial/web # or @mycelial/nodejs
npm install @mycelial/v0
npm install @mycelial/websocket

Import our modules.

import * as Mycelial from '@mycelial/web'; // or '@mycelial/nodejs'
import { Store, Entity } from '@mycelial/v0';
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.

// NOTE: if you use a shared public relay, you'll want to use a unique 
// namespace. The namespace is used as a pub/sub topic, so ensuring it's
// unique will avoid namespace collisions.
const instance = Mycelial.create("mycelial/contacts")

Create and attach a network transport

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

Create a store

const store = new Store(instance);

Subscribe to changes

  • You can use the #subscribe method to listen for changes in the store:
const unsubscribe = store.subscribe((store) => {
console.log('something changed')
});

Create an entity by passing it a unique id followed by an object.

const contact = Entity.from('<unique-contact-id>', {
kind: 'contact',
name: 'James M.',
email: '[email protected]',
phone: '5555555555',
archived: false
});

Add the entity to the store.

store.add(contact);

Query the store

const contacts = store.filter(
(entity) =>
entity.properties.kind === 'contact' &&
entity.properties.archived === false,
);

for (const contact of contacts) {
console.log(contact.properties);
}

Complete Snippet

import * as Mycelial from '@mycelial/web'; // or '@mycelial/nodejs'
import { Store, Entity } from '@mycelial/v0';
import * as Websocket from '@mycelial/websocket';

const instance = Mycelial.create("contacts")

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

const store = new Store(instance);

const unsubscribe = store.subscribe((store) => {
console.log('something changed')
});

const contact = Entity.from('<unique-contact-id>', {
kind: 'contact',
name: 'James M.',
email: '[email protected]',
phone: '5555555555',
archived: false
});

store.add(contact);

const contacts = store.filter(
(entity) =>
entity.properties.kind === 'contact' &&
entity.properties.archived === false,
);

for (const contact of contacts) {
console.log(contact.properties);
}