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

https://github.com/wofwca/webxdc-message-channels

[WIP] Independent message channels for webxdc. No more specifying "message type" and handling all messages in a singular global callback
https://github.com/wofwca/webxdc-message-channels

Last synced: 7 months ago
JSON representation

[WIP] Independent message channels for webxdc. No more specifying "message type" and handling all messages in a singular global callback

Awesome Lists containing this project

README

          

# `webxdc-message-channels`

\[WIP\] Independent message channels for [webxdc][webxdc]. No more specifying "message type" and handling all messages in a singular global callback.

The API is very similar to [WebRTC `RTCDataChannel`s](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel).

## Usage

```javascript
const webxdcTransportDriver = WebxdcNetworkDriver();

const id1 = 1;
const id2 = 2;
const channel1 = webxdcTransportDriver.createChannel(id1);
const channel2 = webxdcNetworkDriver.createChannel(id2);

// When another peer (or us) `sendMessage` on a cannel with the same `id`,
// the listener is invoked.
channel1.setMessageListener((message) => {
console.log('Message on channel 1:', message);
});
channel2.setMessageListener((message) => {
console.log('Message on channel 2:', message);
});

channel1.sendMessage(`to channel 1 from ${webxdc.selfAddr}`);
channel2.sendMessage(`to channel 2 from ${webxdc.selfAddr}`);
```

Expected output (with two peers) (for peer1):

```
Message on channel 1: to channel 1 from peer1@example.com
Message on channel 2: to channel 2 from peer1@example.com
Message on channel 1: to channel 1 from peer2@example.com
Message on channel 2: to channel 2 from peer2@example.com
```

To utilize the original webxdc API's `update.document`, `update.summary`, etc:

```javascript
webxdcTransportDriver.sendMeta({
info: `${webxdc.selfName} edited the document`,
summary: `Last editor: ${webxdc.selfName}`,
document: 'New release',
});
```

[webxdc]: https://webxdc.org/