Skip to content

feat(client-presence): Create Acknowledgment message interface and ackRequested flow #24470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/framework/presence/src/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export type IEphemeralRuntime = Pick<
(IContainerRuntime & IRuntimeInternal) | IFluidDataStoreRuntime,
"clientId" | "connected" | "getAudience" | "getQuorum" | "off" | "on" | "submitSignal"
> &
Partial<Pick<IFluidDataStoreRuntime, "logger">>;
Partial<Pick<IFluidDataStoreRuntime, "logger">> & {
ILayerCompatDetails?: unknown;
};

/**
* @internal
Expand Down
55 changes: 53 additions & 2 deletions packages/framework/presence/src/presenceDatastoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License.
*/

import type { ILayerCompatDetails } from "@fluid-internal/client-utils";
import type { IEmitter } from "@fluidframework/core-interfaces/internal";
import { assert } from "@fluidframework/core-utils/internal";
import type { IInboundSignalMessage } from "@fluidframework/runtime-definitions/internal";
Expand Down Expand Up @@ -75,6 +76,7 @@ interface DatastoreUpdateMessage extends IInboundSignalMessage {
sendTimestamp: number;
avgLatency: number;
isComplete?: true;
acknowledgementId?: number;
data: DatastoreMessageContent;
};
}
Expand All @@ -90,11 +92,33 @@ interface ClientJoinMessage extends IInboundSignalMessage {
};
}

// Message type identifier for presence acknowledgment messages.
const acknowledgementMessageType = "Pres:Ack";

// Signal message format used to acknowledge receipt of presence-related messages.
// These are lightweight and typically contain only the messageId being acknowledged.
interface AcknowledgementMessage extends IInboundSignalMessage {
type: typeof acknowledgementMessageType;
content: {
id: number;
};
}

function isPresenceMessage(
message: IInboundSignalMessage,
): message is DatastoreUpdateMessage | ClientJoinMessage {
): message is DatastoreUpdateMessage | ClientJoinMessage | AcknowledgementMessage {
return message.type.startsWith("Pres:");
}

function isLayerCompatDetails(value: unknown): value is ILayerCompatDetails {
return (
typeof value === "object" &&
value !== null &&
"supportedFeatures" in value &&
value.supportedFeatures instanceof Set
);
}

/**
* @internal
*/
Expand Down Expand Up @@ -356,14 +380,27 @@ export class PresenceDatastoreManagerImpl implements PresenceDatastoreManager {
// IExtensionMessage is a subset of IInboundSignalMessage so this is safe.
// Change types of DatastoreUpdateMessage | ClientJoinMessage to
// IExtensionMessage<> derivatives to see the issues.
message: IInboundSignalMessage | DatastoreUpdateMessage | ClientJoinMessage,
message:
| IInboundSignalMessage
| DatastoreUpdateMessage
| ClientJoinMessage
| AcknowledgementMessage,
local: boolean,
): void {
const received = Date.now();
assert(message.clientId !== null, 0xa3a /* Map received signal without clientId */);
if (!isPresenceMessage(message)) {
return;
}

if (message.type === acknowledgementMessageType) {
// TODO: Handle acknowledgement messages here.
// Placeholder for future implementation.
// These acknowledgement messages will be used to confirm receipt
// of pending messages that were previously sent.
return;
}

if (local) {
const deliveryDelta = received - message.content.sendTimestamp;
// Limit returnedMessages count to 256 such that newest message
Expand Down Expand Up @@ -395,6 +432,20 @@ export class PresenceDatastoreManagerImpl implements PresenceDatastoreManager {
if (message.content.isComplete) {
this.refreshBroadcastRequested = false;
}
// If the message requests an acknowledgement, we will send one back.
if (
message.content.acknowledgementId !== undefined &&
isLayerCompatDetails(this.runtime.ILayerCompatDetails) &&
this.runtime.ILayerCompatDetails?.supportedFeatures?.has("submit_signals_v2") === true
) {
this.runtime.submitSignal(
acknowledgementMessageType,
{
id: message.content.acknowledgementId,
} satisfies AcknowledgementMessage["content"],
message.clientId,
);
}
}

// Handle activation of unregistered workspaces before processing updates.
Expand Down
Loading