Skip to content

Commit

Permalink
Merge pull request #78 from webxdc/hpk/realtime2
Browse files Browse the repository at this point in the history
 `joinRealtimeChannel` method with webxdc realtime API
  • Loading branch information
hpk42 committed May 31, 2024
2 parents 4db7a84 + ac52c6f commit 125a6ce
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
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)
```

0 comments on commit 125a6ce

Please sign in to comment.