@mmstack/mesh 21.8.1 → 21.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -0
- package/fesm2022/mmstack-mesh.mjs +551 -122
- package/fesm2022/mmstack-mesh.mjs.map +1 -1
- package/package.json +2 -2
- package/types/mmstack-mesh.d.ts +181 -4
package/README.md
CHANGED
|
@@ -467,3 +467,87 @@ const mesh = webRtcMesh(store, {
|
|
|
467
467
|
signaling: webSocketTransport('wss://sync.example.com'), // data flows peer to peer
|
|
468
468
|
});
|
|
469
469
|
```
|
|
470
|
+
|
|
471
|
+
### Pointers and other frame-rate values
|
|
472
|
+
|
|
473
|
+
`rtcPresence` is for values that change many times a second and only matter while they are
|
|
474
|
+
fresh: pointer positions, viewports. Each peer publishes one value; every other peer sees the
|
|
475
|
+
latest one per sender. Frames go peer to peer over a lossy, unordered data channel, because a
|
|
476
|
+
pointer that waits for a retransmit is already stale. Nothing is persisted and nothing is caught
|
|
477
|
+
up: a peer that connects sees a sender's value when that sender next moves.
|
|
478
|
+
|
|
479
|
+
It rides on `peerLinks`, the link layer `webRtcMesh` is built on: one peer connection per remote,
|
|
480
|
+
set up from the relay's membership and negotiated over its `signal` frames, carrying the labelled
|
|
481
|
+
channels you ask for. Put `presenceChannel` in the links' channels.
|
|
482
|
+
|
|
483
|
+
```ts
|
|
484
|
+
import {
|
|
485
|
+
peerLinks,
|
|
486
|
+
presenceChannel,
|
|
487
|
+
rtcPresence,
|
|
488
|
+
webSocketTransport,
|
|
489
|
+
} from '@mmstack/mesh';
|
|
490
|
+
|
|
491
|
+
const links = peerLinks({
|
|
492
|
+
room: 'call-7',
|
|
493
|
+
origin: tabId, // one origin is one seat; give every tab its own
|
|
494
|
+
signaling: webSocketTransport('wss://sync.example.com'),
|
|
495
|
+
channels: [presenceChannel],
|
|
496
|
+
});
|
|
497
|
+
const pointers = rtcPresence<{ x: number; y: number }>(links, {
|
|
498
|
+
roster: links.onMembers,
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
pointers.set({ x, y }); // throttled; the first value of a burst goes out at once, the last one lands
|
|
502
|
+
pointers.peers(); // Map of origin to its latest value
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
A document mesh and pointers can share one peer connection: ask `webRtcMesh` for the extra
|
|
506
|
+
channel and hand its links to the presence.
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
const mesh = webRtcMesh(doc, {
|
|
510
|
+
room: 'call-7',
|
|
511
|
+
writer: userId,
|
|
512
|
+
signaling: webSocketTransport('wss://sync.example.com'),
|
|
513
|
+
channels: [presenceChannel],
|
|
514
|
+
});
|
|
515
|
+
const pointers = rtcPresence<Point>(mesh.links, {
|
|
516
|
+
roster: mesh.links.onMembers,
|
|
517
|
+
});
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
The mesh picks a random replica origin per instance unless you pass `origin`; either way
|
|
521
|
+
`mesh.origin` is the key other peers see this side under, and the seat rule below applies.
|
|
522
|
+
|
|
523
|
+
A receiver takes its order only from causality it can observe, never from clocks or from
|
|
524
|
+
numbers chosen by different senders. It holds a value only for an origin in the roster, the
|
|
525
|
+
relay's list of who is in the room, and drops that value as soon as the origin leaves: a peer
|
|
526
|
+
link can outlive a dead socket for a while, so the link is not proof that anyone is there.
|
|
527
|
+
Within one link the highest `seq` wins and anything at or below it is dropped: one counter
|
|
528
|
+
stamps every frame sent on that link in send order, so a lower `seq` was sent earlier. The link
|
|
529
|
+
is the epoch: a link that opens or closes starts its origin fresh, and a frame of an old link
|
|
530
|
+
cannot arrive on a new one. The counter belongs to the links and the channel, not to the sender,
|
|
531
|
+
so a sender closed and recreated on the same links carries on where the last one stopped and is
|
|
532
|
+
heard at once. A new tab or device is a new link, which every receiver already treats as fresh.
|
|
533
|
+
|
|
534
|
+
One origin is one seat. When a second connection arrives with the same origin the relay hands it
|
|
535
|
+
the seat and tells the room about a plain join, so the links tell seats apart by instance: every
|
|
536
|
+
link announces a random instance id in its signals and, once it knows the instance across,
|
|
537
|
+
addresses its signals to it. A signal addressed to an instance a side no longer has is dropped as
|
|
538
|
+
a retired link's leftovers. An unaddressed signal from an instance other than the linked one is a
|
|
539
|
+
new seat, and the receiving side rebuilds its end, addressed, which settles it: only unaddressed
|
|
540
|
+
signals cause a rebuild, and a rebuilt end never sends one. Two tabs sharing an origin still take
|
|
541
|
+
the seat from each other every time one reconnects, so give each tab its own origin.
|
|
542
|
+
|
|
543
|
+
To share a connection someone else already holds, such as a `meshSync` session, pass a
|
|
544
|
+
`SignalingPort` (`send`, `onMessage`, and `members()` over that connection) as `signaling`.
|
|
545
|
+
`members()` returns the current relay roster for the room, or `undefined` until welcomed;
|
|
546
|
+
the owner must keep it current even before the links subscribe. This lets links negotiate with
|
|
547
|
+
existing members when attached after the welcome. Pass the connection's origin to the links;
|
|
548
|
+
they never send a hello of their own.
|
|
549
|
+
|
|
550
|
+
For an external presence roster, pass a subscription function as `roster`. It must synchronously
|
|
551
|
+
emit the current origins and every subsequent membership transition, and return an unsubscribe
|
|
552
|
+
function. Do not adapt a signal using an effect: a leave and rejoin can be coalesced into one
|
|
553
|
+
snapshot, losing the evidence that the previous value must be forgotten.
|