@ably/cli
Version:
Ably CLI for Pub/Sub, Chat and Spaces
278 lines (277 loc) • 15.3 kB
JavaScript
import { RoomStatus, PresenceEventType } from "@ably/chat";
import { Args, Flags } from "@oclif/core";
import chalk from "chalk";
import { ChatBaseCommand } from "../../../chat-base-command.js";
import { waitUntilInterruptedOrTimeout } from "../../../utils/long-running.js";
export default class RoomsPresenceSubscribe extends ChatBaseCommand {
static args = {
roomId: Args.string({
description: "Room ID to subscribe to presence for",
required: true,
}),
};
static description = "Subscribe to presence events in a chat room";
static examples = [
"$ ably rooms presence subscribe my-room",
"$ ably rooms presence subscribe my-room --json",
"$ ably rooms presence subscribe my-room --pretty-json",
];
static flags = {
...ChatBaseCommand.globalFlags,
duration: Flags.integer({
description: "Automatically exit after the given number of seconds (0 = run indefinitely)",
char: "D",
required: false,
}),
};
ablyClient = null;
chatClient = null;
roomId = null;
room = null;
presenceSubscription = null;
unsubscribeStatusFn = null;
cleanupInProgress = false;
commandFlags = null;
async properlyCloseAblyClient() {
const flagsForLog = this.commandFlags || {};
if (!this.ablyClient || this.ablyClient.connection.state === 'closed' || this.ablyClient.connection.state === 'failed') {
this.logCliEvent(flagsForLog, "connection", "alreadyClosedOrFailed", "Ably client already closed or failed, skipping close.");
return;
}
this.logCliEvent(flagsForLog, "connection", "attemptingClose", "Attempting to close Ably client.");
return new Promise((resolve) => {
const timeout = setTimeout(() => {
this.logCliEvent(flagsForLog, "connection", "cleanupTimeout", "Ably client close timed out after 2s. Forcing cleanup.");
resolve();
}, 2000);
const onClosedOrFailed = () => {
clearTimeout(timeout);
this.logCliEvent(flagsForLog, "connection", "closedOrFailedEventFired", `Ably client connection emitted: ${this.ablyClient?.connection.state}`);
resolve();
};
this.ablyClient.connection.once('closed', onClosedOrFailed);
this.ablyClient.connection.once('failed', onClosedOrFailed);
this.ablyClient.close();
});
}
async run() {
const { args, flags } = await this.parse(RoomsPresenceSubscribe);
this.commandFlags = flags;
this.roomId = args.roomId;
try {
// Always show the readiness signal first, before attempting auth
if (!this.shouldOutputJson(flags)) {
// Output the exact signal that E2E tests expect (without ANSI codes)
this.log("Subscribing to presence events. Press Ctrl+C to exit.");
}
// Try to create clients, but don't fail if auth fails
try {
this.chatClient = await this.createChatClient(flags);
this.ablyClient = this._chatRealtimeClient;
}
catch (authError) {
// Auth failed, but we still want to show the signal and wait
this.logCliEvent(flags, "initialization", "authFailed", `Authentication failed: ${authError instanceof Error ? authError.message : String(authError)}`);
if (!this.shouldOutputJson(flags)) {
this.log(chalk.yellow("Warning: Failed to connect to Ably (authentication failed)"));
}
// Wait for the duration even with auth failures
const effectiveDuration = typeof flags.duration === "number" && flags.duration > 0
? flags.duration
: process.env.ABLY_CLI_DEFAULT_DURATION
? Number(process.env.ABLY_CLI_DEFAULT_DURATION)
: undefined;
const exitReason = await waitUntilInterruptedOrTimeout(effectiveDuration);
this.logCliEvent(flags, "presence", "runComplete", "Exiting wait loop (auth exception case)", { exitReason });
this.cleanupInProgress = exitReason === "signal";
return;
}
if (!this.chatClient) {
// Don't exit immediately on auth failures - log the error but continue
this.logCliEvent(flags, "initialization", "failed", "Failed to create Chat client - likely authentication issue");
if (!this.shouldOutputJson(flags)) {
this.log(chalk.yellow("Warning: Failed to connect to Ably (likely authentication issue)"));
}
// Wait for the duration even with auth failures
const effectiveDuration = typeof flags.duration === "number" && flags.duration > 0
? flags.duration
: process.env.ABLY_CLI_DEFAULT_DURATION
? Number(process.env.ABLY_CLI_DEFAULT_DURATION)
: undefined;
const exitReason = await waitUntilInterruptedOrTimeout(effectiveDuration);
this.logCliEvent(flags, "presence", "runComplete", "Exiting wait loop (auth failed case)", { exitReason });
this.cleanupInProgress = exitReason === "signal";
return;
}
// Only proceed with actual functionality if auth succeeded
// Set up connection state logging
this.setupConnectionStateLogging(this.ablyClient, flags, {
includeUserFriendlyMessages: true
});
this.room = await this.chatClient.rooms.get(this.roomId);
const currentRoom = this.room;
this.unsubscribeStatusFn = currentRoom.onStatusChange((statusChange) => {
let reasonDetails;
if (statusChange.current === RoomStatus.Failed) {
reasonDetails = currentRoom.error || undefined;
}
const reasonMsg = reasonDetails instanceof Error ? reasonDetails.message : String(reasonDetails);
this.logCliEvent(flags, "room", `status-${statusChange.current}`, `Room status: ${statusChange.current}`, { reason: reasonMsg });
if (statusChange.current === RoomStatus.Attached && !this.shouldOutputJson(flags) && this.roomId) {
this.log(`${chalk.green("Successfully connected to room:")} ${chalk.cyan(this.roomId)}`);
}
else if (statusChange.current === RoomStatus.Failed && !this.shouldOutputJson(flags)) {
this.error(`Room connection failed: ${reasonMsg || 'Unknown error'}`);
}
});
await currentRoom.attach();
if (!this.shouldOutputJson(flags) && this.roomId) {
this.log(`Fetching current presence members for room ${chalk.cyan(this.roomId)}...`);
const members = await currentRoom.presence.get();
if (members.length === 0) {
this.log(chalk.yellow("No members are currently present in this room."));
}
else {
this.log(`\n${chalk.cyan("Current presence members")} (${chalk.bold(members.length.toString())}):\n`);
for (const member of members) {
this.log(`- ${chalk.blue(member.clientId || "Unknown")}`);
if (member.data && typeof member.data === 'object' && Object.keys(member.data).length > 0) {
const profile = member.data;
if (profile.name) {
this.log(` ${chalk.dim("Name:")} ${profile.name}`);
}
this.log(` ${chalk.dim("Full Profile Data:")} ${this.formatJsonOutput({ data: member.data }, flags)}`);
}
}
}
}
this.logCliEvent(flags, "presence", "subscribingToEvents", "Subscribing to presence events");
this.presenceSubscription = currentRoom.presence.subscribe((event) => {
const timestamp = new Date().toISOString();
const member = event.member;
const eventData = { type: event.type, member: { clientId: member.clientId, data: member.data }, roomId: this.roomId, timestamp };
this.logCliEvent(flags, "presence", event.type, `Presence event '${event.type}' received`, eventData);
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({ success: true, ...eventData }, flags));
}
else {
let actionSymbol = "•";
let actionColor = chalk.white;
if (event.type === PresenceEventType.Enter) {
actionSymbol = "✓";
actionColor = chalk.green;
}
if (event.type === PresenceEventType.Leave) {
actionSymbol = "✗";
actionColor = chalk.red;
}
if (event.type === PresenceEventType.Update) {
actionSymbol = "⟲";
actionColor = chalk.yellow;
}
this.log(`[${timestamp}] ${actionColor(actionSymbol)} ${chalk.blue(member.clientId || "Unknown")} ${actionColor(event.type)}`);
if (member.data && typeof member.data === 'object' && Object.keys(member.data).length > 0) {
const profile = member.data;
if (profile.name) {
this.log(` ${chalk.dim("Name:")} ${profile.name}`);
}
this.log(` ${chalk.dim("Full Profile Data:")} ${this.formatJsonOutput({ data: member.data }, flags)}`);
}
}
});
this.logCliEvent(flags, "presence", "subscribedToEvents", "Successfully subscribed to presence events");
if (!this.shouldOutputJson(flags)) {
this.log(
// Output the exact signal that E2E tests expect (without ANSI codes)
"Subscribing to presence events. Press Ctrl+C to exit.");
}
// Wait until the user interrupts or the optional duration elapses
const effectiveDuration = typeof flags.duration === "number" && flags.duration > 0
? flags.duration
: process.env.ABLY_CLI_DEFAULT_DURATION
? Number(process.env.ABLY_CLI_DEFAULT_DURATION)
: undefined;
const exitReason = await waitUntilInterruptedOrTimeout(effectiveDuration);
this.logCliEvent(flags, "presence", "runComplete", "Exiting wait loop", { exitReason });
this.cleanupInProgress = exitReason === "signal"; // mark if signal so finally knows
}
catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
this.logCliEvent(flags, "presence", "runError", `Error: ${errorMsg}`, { roomId: this.roomId });
if (!this.shouldOutputJson(flags)) {
this.error(`Error: ${errorMsg}`);
}
}
finally {
const currentFlags = this.commandFlags || {};
this.logCliEvent(currentFlags, "presence", "finallyBlockReached", "Reached finally block for presence subscribe.");
if (!this.cleanupInProgress && !this.shouldOutputJson(currentFlags)) {
this.logCliEvent(currentFlags, "presence", "implicitCleanupInFinally", "Performing cleanup (no prior signal).");
}
// Wrap all cleanup in a timeout to prevent hanging
await Promise.race([
this.performCleanup(currentFlags),
new Promise((resolve) => {
setTimeout(() => {
this.logCliEvent(currentFlags, "presence", "cleanupTimeout", "Cleanup timed out after 5s, forcing completion");
resolve();
}, 5000);
})
]);
if (!this.shouldOutputJson(currentFlags)) {
if (this.cleanupInProgress) {
this.log(chalk.green("Graceful shutdown complete (user interrupt)."));
}
else {
this.log(chalk.green("Duration elapsed – command finished cleanly."));
}
}
}
}
async performCleanup(flags) {
// Unsubscribe from presence events with timeout
if (this.presenceSubscription) {
try {
await Promise.race([
Promise.resolve(this.presenceSubscription.unsubscribe()),
new Promise((resolve) => setTimeout(resolve, 1000))
]);
this.logCliEvent(flags, "presence", "unsubscribedEventsFinally", "Unsubscribed presence listener.");
}
catch (error) {
this.logCliEvent(flags, "presence", "unsubscribeErrorFinally", `Error unsubscribing presence subscription: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Unsubscribe from status events with timeout
if (this.unsubscribeStatusFn) {
try {
await Promise.race([
Promise.resolve(this.unsubscribeStatusFn.off()),
new Promise((resolve) => setTimeout(resolve, 1000))
]);
this.logCliEvent(flags, "room", "unsubscribedStatusFinally", "Unsubscribed room status listener.");
}
catch (error) {
this.logCliEvent(flags, "room", "unsubscribeStatusErrorFinally", `Error unsubscribing status listener: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Release room with timeout
if (this.chatClient && this.roomId) {
try {
this.logCliEvent(flags, "room", "releasingFinally", `Releasing room ${this.roomId}.`);
await Promise.race([
this.chatClient.rooms.release(this.roomId),
new Promise((resolve) => setTimeout(resolve, 2000))
]);
this.logCliEvent(flags, "room", "releasedInFinally", `Room ${this.roomId} released.`);
}
catch (error) {
this.logCliEvent(flags, "room", "releaseErrorInFinally", `Error releasing room: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Close Ably client (already has internal timeout)
this.logCliEvent(flags, "connection", "closingClientFinally", "Closing Ably client.");
await this.properlyCloseAblyClient();
this.logCliEvent(flags, "connection", "clientClosedFinally", "Ably client close attempt finished.");
}
}