Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/oakfang/peerq

A p2p queue using a coven broker
https://github.com/oakfang/peerq

Last synced: 6 days ago
JSON representation

A p2p queue using a coven broker

Awesome Lists containing this project

README

        

# peerq
A p2p queue using a coven broker

## Install
`yarn add peerq`

## Usage
```ts
const PeerQueue = require('peerq');

const peerq = new PeerQueue({
host: 'localhost', // Your coven-broker host
port: 4000, // Your coven-broker port
secure: false, // is your broker using wss?
room: 'dev', // Use this coven room for the queue
password: '123456', // encrypt all queue messages with this password (messages that can't be decrypted are dropped)
});

type UUID = string;
type CovenID = UUID;
type PeerqClientID = UUID;
type Timestamp = number;
type PeerqMessagePayload = any;

type PeerqMessage = {
$origin: CovenID;
$id: PeerqClientID;
dt: Timestamp;
topic: string;
payload: PeerqMessagePayload;
};

// peerq.publish([topic: string], payload: PeerqMessagePayload) -> Promise
peerq.publish({ user: 'foo' }).then(() => {
// message was published to queue...
});

// peerq.waitForPeers(peerNumber: number) -> Promise
peerq.waitForPeers(1).then(() => {
// there's at least one other peer using the queue
// this DOES NOT guarantee that peer can read/write your encrypted messages

console.log(peerq.size); // peerq.size -> number, number of active peers
});

// peerq.request([topic: string], payload: PeerqMessagePayload) -> Promise
peerq.request({ user: 'foo' }).then(response => {
// a client responded (see below) to your request.
});

type MessageStreamOptions = {
// exclude your own messages. default: false
excludeSelf: boolean;
};

// peerq.getMessageStream(options?: MessageStreamOptions) -> Observable
const message$ = peerq.getMessageStream()
.filter(({ payload }) => payload.user && payload.user === 'foo')
.observe(async msg => {
// peerq.respond(msg: PeerqMessage, response: PeerqMessagePayload) -> Promise
await peerq.respond(msg, 'meow');
// response sent!
});
```