PeerStore API

API #

PeerStore #

import { PeerStore } from '@galileocap/peer-mesh';
const usePeerStore = new PeerStore();

Used in all other methods. NOTE: You can use usePeerStore.store as a Zustand store, but this is not advised.

init #

usePeerStore.init(dfltPeerState = {}, dfltSharedState = {});

Initializes the store and gets you a peer. Returns a promise that resolves once the peer has been initialized.
The default values will be used when constructing a peer (yours and when connecting to others).

getPeer #

usePeerStore.getPeer(peerId = '<peerId>' | MY_PEER | LEADER_PEER | ALL_PEERS);

Returns the peer with matching id (see: Peers).
WARNING: On React use usePeer.

usePeer #

usePeerStore.usePeer(peerId = '<peerId>' | MY_PEER | LEADER_PEER | ALL_PEERS);

Returns the peer with matching id for React (see: Peers).

sendUpdate #

usePeerStore.sendUpdate(cb = (peer) => {});

Uses the callback function to update your peer’s state and sends that update to the other peers.
The callback function may update the peer’s state or return the new value.
WARNING: Keys starting with '_' will be ignored when sending updates.

getShared #

usePeerStore.getShared();

Returns the shared state.
WARNING: On React use useShared.

useShared #

usePeerStore.useShared();

Returns the shared state.

sharedUpdate #

usePeerStore.sharedUpdate(cb = (sharedState) => {});

Uses the callback function to update the shared state and sends that update to the other peers.
The callback function may update the shared state or return the new value.

connectTo #

usePeerStore.connectTo(peerId = '<peerId>', metadata = {});

Connects to another peer. Returns a promise that resolves once the connection is successful.
The metadata is ignored for now.

sendMessage #

usePeerStore.sendMessage(
  peerId = '<peerId>' | LEADER_PEER | ALL_PEERS,
  type = '<type>',
  data
);

Sends a message of a type to another peer (see subscribeToMessage).
WARNING: Don’t send updates this way, it’s better to have a private property and use sendUpdate.
NOTE: Don’t use the message types '_get', '_set', '_connectTo'.

subscribeToMessage #

usePeerStore.subscribeToMessage(type = '<type>', cb = () => {});

When a message of this type is received the callback function will get fired.
NOTE: Don’t use the message types '_get', '_set', '_connectTo'.

unsubscribeFromMessage #

usePeerStore.unsubscribeFromMessage(type = '<type>');

Undoes subscribeToMessage

Peers #

{
  ...defaultValues, // From init()
  _peer, _conn, // PeerJS peer/connection, for private use only
  _id, // This peer's unique id
  _mine, // True if this is your peer
  _leader // True if this is the leader
}

All values set with sendUpdate will appear here.
WARNING: Keys starting with '_' will be ignored when sending updates.

Utils #

Special id’s #

import { MY_PEER, LEADER_PEER, ALL_PEERS } from '@galileocap/peer-mesh';

There’s a few special peer id’s, these let you get:

  • MY_PEER, returns your peer, beings as undefined until you initialize the store.
  • LEADER_PEER, returns the peer that’s the leader.
  • ALL_PEERS, returns an array of peers instead.