Skip to content

Commit f82d8fe

Browse files
committed
docs: add explanations for isAppSender/isBroadcast across spec and shared state chapters
1 parent b2826d4 commit f82d8fe

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

src-docs/shared_state/conflicts.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ both a Total Order of messages and a "single source of truth" can not be assumed
2929
Many webxdc applications are incapable of producing conflicts,
3030
either because they do not use the network communication APIs,
3131
or because any given data structure can only have one writer.
32+
Alternatively, an application can designate a "central" authority
33+
by checking [`webxdc.isAppSender`](../spec/selfAddr_and_selfName.html#isappsender)
34+
to identify whether it is running for the user who shared the app.
35+
This peer could then authoritatively resolve conflicts or coordinate state updates.
36+
However, developers should be aware that if the app depends entirely on the sender being online,
37+
the app's availability will be limited when the sender is offline.
38+
3239
For example, many of the available [webxdc games](https://webxdc.org/apps/) offer a high-score table
3340
and how users scored will typically be reported back to the chat.
3441
Such a gaming app will simply post their user's highscore

src-docs/shared_state/crdts.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ While this behaviour can be very helpful for application developers,
148148
it may not free you entirely from having to think about network conditions.
149149
_Eventually-consistent_ application state should generally be treated as
150150
subjective, which can be a significant shift if you are used to having a server acting as an authority.
151+
While you can use [`webxdc.isAppSender`](../spec/selfAddr_and_selfName.html#isappsender) to nominate a peer
152+
(the app sender) to act as a kind of server, this introduces a dependency on that peer being online.
151153
That means that conditional behaviour that you'd usually treat as _yes_ and _no_,
152154
may instead behave more like _currently_ and _not yet_.
153155

src-docs/shared_state/practical.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,25 @@ As for the matter of duplicate ids in the `order` array,
473473
the rendering code which constructs the app's UI from this data
474474
could ignore repeated elements when iterating over them.
475475

476+
## Centralized coordination
477+
478+
While CRDTs are designed for peer-to-peer operation,
479+
you may sometimes want to designate one client as a "coordinator".
480+
By checking [`webxdc.isAppSender`](../spec/selfAddr_and_selfName.html#isappsender),
481+
an app can identify whether it is running for the peer
482+
that initially shared the app.
483+
This "sender" instance can then take on special roles,
484+
such as authoritatively resolving state conflicts,
485+
for example by letting the app sender's updates always win.
486+
487+
However, use this model with care:
488+
if your app logic requires the sender to be online to function,
489+
it will be unusable whenever the sender is offline or has left the chat.
490+
One further complication occurs if the app sender
491+
has multiple devices running the app simultaneously,
492+
with each device possibly regarding itself as the central coordinator
493+
and producing conflicting application states.
494+
476495
## Learning more
477496

478497
Many more examples can be found throughout

src-docs/spec/selfAddr_and_selfName.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ window.webxdc.isAppSender
4242
who initially shared the webxdc application in the chat.
4343
If it is `false`, the user is a receiver.
4444

45+
This supports a common development model where a "central" app instance
46+
(the sender's) processes all updates and distributes the resulting state
47+
back to all peers.
48+
4549
## isBroadcast
4650

4751
```js
@@ -51,6 +55,12 @@ window.webxdc.isBroadcast
5155
`isBroadcast` is a boolean value that is `true` if the webxdc
5256
application is shared in a broadcast chat context.
5357

58+
On some platforms, such as "broadcast channels," it is technically impossible
59+
for subscribers to discover or send updates to each other directly.
60+
In those cases, [updates](./sendUpdate.md) sent by a non-sender
61+
will only be seen by the app sender,
62+
who alone can distribute updates globally.
63+
5464

5565
## Example using selfAddr and selfName
5666

src-docs/spec/sendUpdate.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
window.webxdc.sendUpdate(update, descr);
55
```
66

7-
Send an update to all peers.
7+
Send an update to peers.
8+
If `webxdc.isBroadcast` is false, the update is sent to all peers.
9+
If `webxdc.isBroadcast` is true and `webxdc.isAppSender` is false,
10+
the update is only sent to the app sender.
811

9-
All peers, including the sending one,
12+
All receiving peers, including the sending one,
1013
will receive the update by the callback given to [`setUpdateListener()`](./setUpdateListener.html).
1114

1215
There are situations where the user cannot send messages to a chat,

0 commit comments

Comments
 (0)