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

feat: ephemeral peer channel #75

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 7 additions & 6 deletions src-docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
- [Webxdc Specification](./spec/README.md)
- [Container file format (`.xdc`)](./spec/format.md)
- [Javascript API](./spec/api.md)
- [sendUpdate](./spec/sendUpdate.md)
- [setUpdateListener](./spec/setUpdateListener.md)
- [sendToChat](./spec/sendToChat.md)
- [importFiles](./spec/importFiles.md)
- [selfAddr & selfName](./spec/selfAddr_and_selfName.md)
- [sendUpdate](./spec/sendUpdate.md)
- [setUpdateListener](./spec/setUpdateListener.md)
- [sendEphemeral](./spec/sendEphemeral.md)
- [setEphemeralListener](./spec/setEphemeralListener.md)
- [sendToChat](./spec/sendToChat.md)
- [importFiles](./spec/importFiles.md)
- [selfAddr & selfName](./spec/selfAddr_and_selfName.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
9 changes: 9 additions & 0 deletions src-docs/spec/sendEphemeral.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# sendEphemeral

```js
window.webxdc.sendEphemeral(payload);
```

Send an ephemeral message to all peers. [setEphemeralListener](./setEphemeralListener.md) has to be called first to create a connection to at least on peer. See the documentation for [setEphemeralListener](./setEphemeralListener.md) to find out more.

- `payload` any javascript object that can be given to `JSON.stringify`
31 changes: 31 additions & 0 deletions src-docs/spec/setEphemeralListener.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# setEphemeralListener

```js
window.webxdc.setEphemeralListener((payload) => {});
```

`setEphemeralListener` can be used to set a callback that receives the _ephemeral_ messages
sent by [`sendEphemeral`](./sendEphemeral.md). Ephemeral messages are messages that are delivered only to peers that are currently connected with a direct connection to the sender. These messages are not persisted and should thus only be used for unimportant synchronization like cursor positions and live game data. Members of a chat that are not currently connected will never receive these messages.
The `setEphemeralListener` function returnes a promise that resolves as soon as at least one peer is online. The completion of the promise thus signales that the connection is usable for message delivery. Messages that are send with [`sendEphemeral`](./sendEphemeral.md) before this promise resolves are discarded and will never reach any peer.

- `payload`: Any json object deserialized by `JSON.parse`.

Calling `setEphemeralListener()` multiple times is undefined behavior: in current implementations the callback is simply replaced.


## Example
```js
// stub implementation until the channel is ready
let sendGossip = () => { console.error("transport not ready") }

window.webxdc.setEphemeralListener(function (message) {
console.log("Received ephemeral message: ", message);
/* Application code */
}).then(() => {
// Replace `sendGossip` with proper implementation
sendGossip = () => {
console.log("New ephemeral message: ", msg);
window.webxdc.sendEphemeral(msg);
}
});
```