Skip to content

joinRealtimeChannel method with webxdc realtime API #78

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

Merged
merged 23 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
- [sendToChat](./spec/sendToChat.md)
- [importFiles](./spec/importFiles.md)
- [selfAddr & selfName](./spec/selfAddr_and_selfName.md)
- [joinRealtimeChannel](./spec/joinRealtimeChannel.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
69 changes: 69 additions & 0 deletions src-docs/spec/joinRealtimeChannel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# joinRealtimeChannel (experimental)

```js
const realtimeChannel = window.webxdc.joinRealtimeChannel();
```

Setup and return the realtime channel for this app,
with methods for listening and sending data as well as leaving the channel.
Per-app realtime channels are:

- **private**: no one outside the chat can participate in realtime channels.

- **isolated**: apps can not participate in realtime channels of other apps.

- **ephemeral**: any sent data will only be received by currently
connected peers but not by peers connecting later.

Calling `joinRealtimeChannel` a second time without leaving the prior one
will throw an error.

## `realtimeChannel.setListener((data) => {})`

Start listening on the realtime channel using the specified callback.
The callback receives `Uint8Array` data items that were sent from connected peers.
Calling `setListener` a second time will replace the previous listener.


## `realtimeChannel.send(data)`

Send a `Uint8Array` data item to connected peers.
There is no guarantee anyone is receiving sent data
because there might be no currently listening peers,
or network connections fail.
It is up to the app to determine connectivity status with other peers
by monitoring and triggering data messages.


## `realtimeChannel.leave()`

Leave the realtime channel.
Afterwards the `realtimeChannel` is invalid and
can not be used anymore for sending or receiving data.
You need to call `window.webxdc.joinRealtimeChannel()` again
to re-join the per-app realtime channel.

## Example

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

let numMsgs = 0
const refreshIntervalId = setInterval(() => {
const myId = window.webxdc.selfAddr;
const data = new TextEncoder().encode(`[${numMsgs}] hello from ${myId}`);
numMsgs += 1
console.log("Sending message", data);
realtimeChannel.send(data);
if (numMsgs >= 100) {
realtimeChannel.leave();
clearInterval(refreshIntervalId);
}

}, 1000)
```