Skip to main content


PubSub allows to publish and subscribe to events in topics

All events is serialized using JSON.parse / JSON.stringify methods

See Peerchat demo


Topics is a scope for event to happen, you can publish or subscribe to events in it

Get available topics

const { pubsub } = stack;
const topics = await pubsub.topics();

console.log(topics); // Example output: `['topic1', 'topic2']`

Get peers count that subscribed to topic

const { pubsub } = stack;
const count = await pubsub.peers('topic1');

console.log(topics); // Example output: 1

Reserved topic prefixes

You cannot use $$ at start of topic name, it is reserved for internal usage, for example for store replication.

Topics prefixed with $$ will not be shown in available topics


Receive event in topic

const { pubsub } = stack;
pubsub.subscribe('topic1', (msg) => {
  console.log(msg.from,; // Example output: `somePeerId {hello: 'world'}`


Will stop receiving events from topic

const { pubsub } = stack;


Create event in topic

const { pubsub } = stack;
pubsub.publish('topic1', { hello: 'world' });

Wait for peers to appear in topic before publish:

const { pubsub } = stack;
pubsub.publish('topic1', { hello: 'world' }, false);

Create sub-instance

All topics and events in it will be scoped to new instance

const pubsub = stack.pubsub.create('sub-pubsub');