Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce experimental real time channels #77

Closed
wants to merge 18 commits into from
3 changes: 2 additions & 1 deletion src-docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
- [sendToChat](./spec/sendToChat.md)
- [importFiles](./spec/importFiles.md)
- [selfAddr & selfName](./spec/selfAddr_and_selfName.md)
- [setRealtimeListener](./spec/setRealtimeListener.md)
- [sendRealtimeData](./spec/sendRealtimeData.md)
- [Messenger implementations](./spec/messenger.md)

- [Shared Web Application state](./shared_state/README.md)
- [Detecting conflicts](./shared_state/conflicts.md)
- [Theory of Conflict-free Replicated Data Types (CRDTs)](./shared_state/crdts.md)
Expand Down
48 changes: 48 additions & 0 deletions src-docs/spec/sendRealtimeData.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# sendRealtimeData()

```js
window.webxdc.sendRealtimeData(data);
```

**Note that this API is experimental and not fully settled (April 2024)**

Send `Uint8Array` data to other realtime peers for this app.
This will implicitely trigger the setup of realtime connections
if they are not established yet.
Note that any data sent before a first peer is connected might not arrive.
See [`setRealtimeListener`](./setRealtimeListener.md) for how data can be received.
It is up to the app to implement a "synchronization" protocol
so that peers can detect each other's presence.

Any sent data is

- **private to the chat**: Only chat members can receive realtime data.

- **scoped to the app**: different apps in a
chat can not discover or receive realtime data of other apps in the chat.

- **ephemeral**: any sent data will only be received by the currently
connected chat peers but not by peers joining later.
There is no guarantee anyone is receiving the sent data
because there might be no currently listening peers,
or network connections broke.

## Example

```js

window.webxdc.setRealtimeListener((data) => {
console.log("Received realtime data: ", data);
const msg = new TextDecoder().decode(data);
console.log("decoded message: ", msg);
})

let pings = 0
setInterval(() => {
const myId = window.webxdc.selfAddr;
const data = new TextEncoder().encode(`[${pings}] hello from ${myId}`);
pings += 1
console.log("Sending message", data);
window.webxdc.sendRealtimeData(data);
}, 1000)
```
38 changes: 38 additions & 0 deletions src-docs/spec/setRealtimeListener.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# setRealtimeListener

```js
window.webxdc.setRealtimeListener((data) => {});
```

**Note that this API is experimental and not fully settled (April 2024)**

Register a listener callback for realtime data arriving from other peers.
This will implicitely trigger the setup of realtime connections
if they were not established yet.
The `setRealtimeListener` callback receives `Uint8Array` data items
that were sent from connected peers.
See [`sendRealtimeData(data)`](./sendRealtimeData.md) for more info.

Calling `setRealtimeListener` with a `null` value
will disconnect from receiving and sending realtime data.
You may afterwards call `setRealtimeListener` again with a callback
to re-establish participation in realtime sending and receiving.

## Example

```js
window.webxdc.setRealtimeListener((data) => {
console.log("Received realtime data: ", data);
const msg = new TextDecoder().decode(data);
console.log("decoded message: ", msg);
})

let pings = 0
setInterval(() => {
const myId = window.webxdc.selfAddr;
const data = new TextEncoder().encode(`[${pings}] hello from ${myId}`);
pings += 1
console.log("Sending message", data);
window.webxdc.sendRealtimeData(data);
}, 1000)
```