|
495 | 495 | * you have configured. See the example below. |
496 | 496 | * @param webSocketServer A WebSocketServer object from your server environment. |
497 | 497 | * @param createPersisterForPath An optional function that will create a |
498 | | - * MergeableStore and a Persister to synchronize with the clients on a given |
| 498 | + * Persister (with a MergeableStore) to synchronize with the clients on a given |
499 | 499 | * path. |
500 | 500 | * @returns A reference to the new WsServer object. |
501 | 501 | * @example |
502 | | - * This example creates a WsServer and then destroys it again. |
| 502 | + * This example creates a WsServer that synchronizes two clients on a shared |
| 503 | + * path. |
503 | 504 | * |
504 | 505 | * ```js |
505 | 506 | * import {WebSocketServer} from 'ws'; |
| 507 | + * import {createMergeableStore} from 'tinybase'; |
506 | 508 | * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server'; |
| 509 | + * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client'; |
507 | 510 | * |
| 511 | + * // Server |
508 | 512 | * const server = createWsServer(new WebSocketServer({port: 8047})); |
| 513 | + * |
| 514 | + * // Client 1 |
| 515 | + * const clientStore1 = createMergeableStore(); |
| 516 | + * clientStore1.setCell('pets', 'fido', 'species', 'dog'); |
| 517 | + * const synchronizer1 = await createWsSynchronizer( |
| 518 | + * clientStore1, |
| 519 | + * new WebSocket('ws://localhost:8047/petShop'), |
| 520 | + * ); |
| 521 | + * await synchronizer1.startSync(); |
| 522 | + * // ... |
| 523 | + * |
| 524 | + * // Client 2 |
| 525 | + * const clientStore2 = createMergeableStore(); |
| 526 | + * clientStore2.setCell('pets', 'felix', 'species', 'cat'); |
| 527 | + * const synchronizer2 = await createWsSynchronizer( |
| 528 | + * clientStore2, |
| 529 | + * new WebSocket('ws://localhost:8047/petShop'), |
| 530 | + * ); |
| 531 | + * await synchronizer2.startSync(); |
| 532 | + * // ... |
| 533 | + * |
| 534 | + * console.log(clientStore1.getTables()); |
| 535 | + * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}} |
| 536 | + * |
| 537 | + * console.log(clientStore2.getTables()); |
| 538 | + * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}} |
| 539 | + * |
| 540 | + * synchronizer1.destroy(); |
| 541 | + * synchronizer2.destroy(); |
509 | 542 | * server.destroy(); |
510 | 543 | * ``` |
511 | 544 | * @example |
| 545 | + * This longer example creates a WsServer that persists a MergeableStore to file |
| 546 | + * that is synchronized with two clients on a shared path. Later, when a third |
| 547 | + * client connects, it picks up the data the previous two were using. |
| 548 | + * |
| 549 | + * ```js |
| 550 | + * import {WebSocketServer} from 'ws'; |
| 551 | + * import {createFilePersister} from 'tinybase/persisters/persister-file'; |
| 552 | + * import {createMergeableStore} from 'tinybase'; |
| 553 | + * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server'; |
| 554 | + * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client'; |
| 555 | + * import {rmSync} from 'fs'; |
| 556 | + * |
| 557 | + * // Server |
| 558 | + * const server = createWsServer( |
| 559 | + * new WebSocketServer({port: 8047}), |
| 560 | + * (pathId) => |
| 561 | + * createFilePersister(createMergeableStore(), pathId + '.json'), |
| 562 | + * ); |
| 563 | + * |
| 564 | + * // Client 1 |
| 565 | + * const clientStore1 = createMergeableStore(); |
| 566 | + * clientStore1.setCell('pets', 'fido', 'species', 'dog'); |
| 567 | + * const synchronizer1 = await createWsSynchronizer( |
| 568 | + * clientStore1, |
| 569 | + * new WebSocket('ws://localhost:8047/petShop'), |
| 570 | + * ); |
| 571 | + * await synchronizer1.startSync(); |
| 572 | + * // ... |
| 573 | + * |
| 574 | + * // Client 2 |
| 575 | + * const clientStore2 = createMergeableStore(); |
| 576 | + * clientStore2.setCell('pets', 'felix', 'species', 'cat'); |
| 577 | + * const synchronizer2 = await createWsSynchronizer( |
| 578 | + * clientStore2, |
| 579 | + * new WebSocket('ws://localhost:8047/petShop'), |
| 580 | + * ); |
| 581 | + * await synchronizer2.startSync(); |
| 582 | + * // ... |
| 583 | + * |
| 584 | + * console.log(clientStore1.getTables()); |
| 585 | + * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}} |
| 586 | + * |
| 587 | + * console.log(clientStore2.getTables()); |
| 588 | + * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}} |
| 589 | + * |
| 590 | + * synchronizer1.destroy(); |
| 591 | + * synchronizer2.destroy(); |
| 592 | + * |
| 593 | + * // ... |
| 594 | + * // Client 3 connects later |
| 595 | + * const clientStore3 = createMergeableStore(); |
| 596 | + * const synchronizer3 = await createWsSynchronizer( |
| 597 | + * clientStore3, |
| 598 | + * new WebSocket('ws://localhost:8047/petShop'), |
| 599 | + * ); |
| 600 | + * await synchronizer3.startSync(); |
| 601 | + * // ... |
| 602 | + * |
| 603 | + * console.log(clientStore3.getTables()); |
| 604 | + * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}} |
| 605 | + * |
| 606 | + * synchronizer3.destroy(); |
| 607 | + * server.destroy(); |
| 608 | + * |
| 609 | + * // Remove file for the purposes of this demo. |
| 610 | + * rmSync('petShop.json'); |
| 611 | + * ``` |
| 612 | + * @example |
512 | 613 | * This example creates a WsServer with a custom listener that displays |
513 | 614 | * information about the address of the client that connects to it. |
514 | 615 | * |
|
0 commit comments