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
- Host: GitHub
- URL: https://github.com/wofwca/webxdc-message-channels
- Owner: WofWca
- Created: 2023-09-16T16:42:13.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-09-21T17:35:21.000Z (about 2 years ago)
- Last Synced: 2025-03-02T01:40:02.539Z (7 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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/