Sharing a space
By the end of this page a second peer will have joined the app’s space, and each peer will see the other’s edits.
Before you start
Section titled “Before you start”A space is shared by invitation. The host creates an invitation, and the guest uses it to find the host and open a secure connection. The host then gives the guest an authentication code, which the guest sends back over that connection to prove it received the invitation from the host.
Once the guest is in, both peers hold a full replica of the space’s database. Each peer reads and writes its own replica, and the replicas exchange changes whenever the peers can reach each other. A peer that goes offline keeps working, and catches up when it reconnects.
Spaces and databases explains the handshake in detail.
-
Invite a peer.
space.sharestarts an invitation. Its code locates you on the network, and its authentication code proves the guest received the invitation from you.const {space } = awaitconst space: SpaceopenContacts();function openContacts(services?: ClientServicesProvider): Promise<{client: Client;space: Space;}>Starts a client that knows the app's types, gives it an identity, and creates the space the app keeps its records in.
constinvitation =const invitation: CancellableInvitationspace.const space: Spaceshare();Space.share(options?: Partial<Invitation>): CancellableInvitation// Send this code to the guest; it locates the host and opens a secure connection.constcode =const code: stringInvitationEncoder.class InvitationEncoderEncodes and decodes an invitation proto into/from alphanumeric chars.
encode(InvitationEncoder.encode(invitation: Invitation): stringinvitation.const invitation: CancellableInvitationget());MulticastObservable<Invitation>.get(): InvitationGet the current value of the observable.
// Pass this second factor over another channel, such as a QR code or a call.constauthCode =const authCode: string | undefinedinvitation.const invitation: CancellableInvitationget().MulticastObservable<Invitation>.get(): InvitationGet the current value of the observable.
authCode;authCode?: string | undefined/ Authentication code created by host (only present if auth_method is SHARED_SECRET).
Send the two codes over different channels. Someone who intercepts one of them cannot join.
-
Join from the other peer. The guest needs a client that knows the app’s types and an identity of its own. It decodes the invitation, connects, and sends the authentication code back.
constguest = newconst guest: ClientClient({new Client(options?: ClientOptions): ClientThe Client class encapsulates the core client-side API of DXOS.
types });types?: AnyEntity[] | undefinedECHO schema.
awaitguest.const guest: Clientinitialize();Client.initialize(): Promise<Client>Initializes internal resources in an idempotent way. Required before using the Client instance.
awaitguest.const guest: Clienthalo.Client.halo: HaloHALO credentials.
createIdentity();Halo.createIdentity(options?: ProfileDocument, deviceProfile?: DeviceProfileDocument): Promise<Identity>constaccepted =const accepted: AuthenticatingInvitationguest.const guest: Clientspaces.Client.spaces: EchoECHO Spaces.
join(Echo.join(invitation: Invitation | string): AuthenticatingInvitationJoins an existing space using the given invitation.
InvitationEncoder.class InvitationEncoderEncodes and decodes an invitation proto into/from alphanumeric chars.
decode(InvitationEncoder.decode(text: string): Invitationcode));const code: stringawaitaccepted.const accepted: AuthenticatingInvitationauthenticate(AuthenticatingInvitation.authenticate(authCode: string): Promise<void>authCode!);const authCode: string | undefined -
Find the space. Once authentication completes, the space is among the guest’s spaces, and both peers replicate it.
// The space appears among the guest's spaces once the invitation succeeds.constspaces =const spaces: Space[]guest.const guest: Clientspaces.Client.spaces: EchoECHO Spaces.
get();Echo.get(): Space[] (+2 overloads)Returns the list of spaces.
Try it
Section titled “Try it”Alice created the space and Bob joined it by invitation. Edit a record on one side and watch the other follow. Switch a peer offline, edit on both sides, then switch it back on: the replicas exchange what they missed.
What you can do now
Section titled “What you can do now”You can invite a peer to a space, accept the invitation from a second client, and edit one database from both.
Spaces and databases: what a space is, and how peers join one.