Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand {
flags: BaseFlags,
options?: {
skipAuthInfo?: boolean;
autoConnect?: boolean;
},
): Promise<Ably.Realtime | null> {
// Return cached client if it exists
Expand All @@ -348,6 +349,7 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand {
const client = await this.createAblyClientInternal(flags, {
type: "realtime",
skipAuthInfo: options?.skipAuthInfo,
autoConnect: options?.autoConnect,
});

// Cache the client for reuse
Expand All @@ -367,6 +369,7 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand {
options?: {
type?: "rest" | "realtime";
skipAuthInfo?: boolean;
autoConnect?: boolean;
},
): Promise<Ably.Rest | Ably.Realtime | null> {
const clientType = options?.type || "realtime";
Expand Down Expand Up @@ -438,7 +441,16 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand {
}

// Create Realtime client
const client = new Ably.Realtime(clientOptions);
const autoConnect = options?.autoConnect !== false;
const client = new Ably.Realtime({
...clientOptions,
autoConnect,
});

// If autoConnect is disabled, return the client immediately without waiting for connection
if (!autoConnect) {
return client;
}

// Wait for the connection to be established or fail
return await new Promise((resolve, reject) => {
Expand Down
7 changes: 6 additions & 1 deletion src/chat-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ export abstract class ChatBaseCommand extends AblyBaseCommand {
*/
protected async createChatClient(
flags: BaseFlags,
options?: { restOnly?: boolean },
): Promise<ChatClient | null> {
// We already have a client, return it
if (this._chatClient) {
return this._chatClient;
}

// Create Ably Realtime client first
const realtimeClient = await this.createAblyRealtimeClient(flags);
// When restOnly is true, skip auto-connect to avoid opening a WebSocket
// for commands that only use REST API methods (send, delete, update, history, etc.)
const realtimeClient = await this.createAblyRealtimeClient(flags, {
autoConnect: options?.restOnly ? false : undefined,
});

// Mark auth info as shown after creating the client
// to prevent duplicate "Using..." output on subsequent calls
Expand Down
4 changes: 1 addition & 3 deletions src/commands/rooms/messages/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class MessagesDelete extends ChatBaseCommand {
const { args, flags } = await this.parse(MessagesDelete);

try {
const chatClient = await this.createChatClient(flags);
const chatClient = await this.createChatClient(flags, { restOnly: true });

if (!chatClient) {
return this.fail(
Expand All @@ -51,8 +51,6 @@ export default class MessagesDelete extends ChatBaseCommand {
);
}

this.setupConnectionStateLogging(chatClient.realtime, flags);

const room = await chatClient.rooms.get(args.room);

if (!this.shouldOutputJson(flags)) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/rooms/messages/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class MessagesHistory extends ChatBaseCommand {

try {
// Create Chat client
const chatClient = await this.createChatClient(flags);
const chatClient = await this.createChatClient(flags, { restOnly: true });

if (!chatClient) {
return this.fail(
Expand Down
18 changes: 1 addition & 17 deletions src/commands/rooms/messages/reactions/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class MessagesReactionsRemove extends ChatBaseCommand {

try {
// Create Chat client
const chatClient = await this.createChatClient(flags);
const chatClient = await this.createChatClient(flags, { restOnly: true });

if (!chatClient) {
return this.fail(
Expand All @@ -58,9 +58,6 @@ export default class MessagesReactionsRemove extends ChatBaseCommand {
);
}

// Set up connection state logging
this.setupConnectionStateLogging(chatClient.realtime, flags);

// Get the room
this.logCliEvent(
flags,
Expand All @@ -71,19 +68,6 @@ export default class MessagesReactionsRemove extends ChatBaseCommand {
const chatRoom = await chatClient.rooms.get(room);
this.logCliEvent(flags, "room", "gotRoom", `Got room handle for ${room}`);

// Subscribe to room status changes
this.setupRoomStatusHandler(chatRoom, flags, { roomName: room });

// Attach to the room
this.logCliEvent(flags, "room", "attaching", `Attaching to room ${room}`);
await chatRoom.attach();
this.logCliEvent(
flags,
"room",
"attached",
`Successfully attached to room ${room}`,
);

// Remove the reaction
this.logCliEvent(
flags,
Expand Down
18 changes: 1 addition & 17 deletions src/commands/rooms/messages/reactions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class MessagesReactionsSend extends ChatBaseCommand {
}

// Create Chat client
this.chatClient = await this.createChatClient(flags);
this.chatClient = await this.createChatClient(flags, { restOnly: true });

if (!this.chatClient) {
return this.fail(
Expand All @@ -79,9 +79,6 @@ export default class MessagesReactionsSend extends ChatBaseCommand {
);
}

// Set up connection state logging
this.setupConnectionStateLogging(this.chatClient.realtime, flags);

// Get the room
this.logCliEvent(
flags,
Expand All @@ -92,19 +89,6 @@ export default class MessagesReactionsSend extends ChatBaseCommand {
const chatRoom = await this.chatClient.rooms.get(room);
this.logCliEvent(flags, "room", "gotRoom", `Got room handle for ${room}`);

// Subscribe to room status changes
this.setupRoomStatusHandler(chatRoom, flags, { roomName: room });

// Attach to the room
this.logCliEvent(flags, "room", "attaching", `Attaching to room ${room}`);
await chatRoom.attach();
this.logCliEvent(
flags,
"room",
"attached",
`Successfully attached to room ${room}`,
);

// Prepare the reaction parameters
const reactionParams: {
name: string;
Expand Down
5 changes: 1 addition & 4 deletions src/commands/rooms/messages/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class MessagesSend extends ChatBaseCommand {

try {
// Create Chat client
this.chatClient = await this.createChatClient(flags);
this.chatClient = await this.createChatClient(flags, { restOnly: true });

if (!this.chatClient) {
return this.fail(
Expand All @@ -108,9 +108,6 @@ export default class MessagesSend extends ChatBaseCommand {
);
}

// Set up connection state logging
this.setupConnectionStateLogging(this.chatClient.realtime, flags);

// Parse metadata if provided
let metadata: JsonObject | undefined;
if (flags.metadata) {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/rooms/messages/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class MessagesUpdate extends ChatBaseCommand {
);
}

const chatClient = await this.createChatClient(flags);
const chatClient = await this.createChatClient(flags, { restOnly: true });

if (!chatClient) {
return this.fail(
Expand All @@ -130,8 +130,6 @@ export default class MessagesUpdate extends ChatBaseCommand {
);
}

this.setupConnectionStateLogging(chatClient.realtime, flags);

const room = await chatClient.rooms.get(args.room);

if (!this.shouldOutputJson(flags)) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/rooms/occupancy/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class RoomsOccupancyGet extends ChatBaseCommand {

try {
// Create Chat client
this.chatClient = await this.createChatClient(flags);
this.chatClient = await this.createChatClient(flags, { restOnly: true });

if (!this.chatClient) {
return this.fail(
Expand Down
3 changes: 0 additions & 3 deletions src/commands/rooms/reactions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ export default class RoomsReactionsSend extends ChatBaseCommand {
);
}

// Set up connection state logging
this.setupConnectionStateLogging(this.chatClient.realtime, flags);

// Get the room
this.logCliEvent(
flags,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/commands/rooms/messages/reactions/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("rooms:messages:reactions:remove command", () => {
import.meta.url,
);

expect(room.attach).toHaveBeenCalled();
expect(room.attach).not.toHaveBeenCalled();
expect(room.messages.reactions.delete).toHaveBeenCalledWith(
"msg-serial-123",
{
Expand Down
2 changes: 1 addition & 1 deletion test/unit/commands/rooms/messages/reactions/send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("rooms:messages:reactions:send command", () => {
import.meta.url,
);

expect(room.attach).toHaveBeenCalled();
expect(room.attach).not.toHaveBeenCalled();
expect(room.messages.reactions.send).toHaveBeenCalledWith(
"msg-serial-123",
{
Expand Down
Loading