-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from webxdc/hpk/realtime2
`joinRealtimeChannel` method with webxdc realtime API
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |