Provides direct access to the underlying Ably Realtime channel.
Use this for advanced scenarios requiring direct access to the underlying channel. Directly interacting with the Ably channel can lead to unexpected behavior, and so is generally discouraged.
The underlying Ably RealtimeChannel instance
The error that caused the room to enter its current status, if any.
ErrorInfo if an error caused the current status, undefined otherwise
const room = await chatClient.rooms.get('private-chat');
if (room.error) {
console.error('Room error:', room.error.message);
console.error('Error code:', room.error.code);
// Handle specific error codes
if (room.error.code === 40300) {
showMessage('Access denied to this room');
} else {
showMessage(`Connection failed: ${room.error.message}`);
}
}
Provides access to the messages feature for sending, receiving, and querying chat messages.
The Messages instance for this room
Provides access to room occupancy metrics for tracking connection and presence counts.
The Occupancy instance for this room
Provides access to the presence feature for tracking user presence state.
The Presence instance for this room
Provides access to room-level reactions for sending ephemeral reactions.
The RoomReactions instance for this room
Provides access to the typing indicators feature for showing who is currently typing.
The Typing instance for this room
Attaches to the room to begin receiving events.
Establishes an attachment to the room, enabling message delivery,
presence updates, typing, and other events. The room must be
attached before non-REST-based operations (like presence.enter()) can be performed.
Note:
Promise that resolves when the room is successfully attached, or rejects with:
import * as Ably from 'ably';
import { ChatClient, RoomStatus } from '@ably/chat';
const chatClient: ChatClient; // existing ChatClient instance
const room = await chatClient.rooms.get('team-standup');
// Attach to room with error handling
try {
await room.attach();
console.log('Successfully attached to room');
// Now safe to use room features
await room.presence.enter();
// And subscriptions will start receiving events
room.messages.subscribe((event) => {
console.log('New message:', event.message);
});
} catch (error) {
console.error('Failed to attach to room:', error);
// Check current room status
if (room.status === RoomStatus.Suspended) {
console.log('Room suspended, will retry automatically');
} else if (room.status === RoomStatus.Failed) {
console.error('Room failed, manual intervention needed');
}
}
Detaches from the room to stop receiving chat events.
Subscriptions remain registered but won't receive events until the room is reattached. Use this to gracefully detach when leaving a chat view. This command leaves all subscriptions intact, so they will resume receiving events when the room is reattached.
Promise that resolves when the room is successfully detached
import * as Ably from 'ably';
import { ChatClient } from '@ably/chat';
const chatClient: ChatClient; // existing ChatClient instance
// Get a room with default options and attach to it
const room = await chatClient.rooms.get('customer-support');
await room.attach();
// Do chat operations...
try {
// Detach from room
await room.detach();
console.log('Successfully detached from room');
} catch (error) {
console.error('Failed to detach from room:', error);
}
Registers a handler for discontinuity events in the room's connection.
A discontinuity occurs when the connection is interrupted and cannot resume from its previous state, potentially resulting in missed messages or events. Use this to detect gaps in the event stream and take corrective action.
Note:
Callback invoked when a discontinuity is detected
Subscription object with an unsubscribe method
import * as Ably from 'ably';
import { ChatClient } from '@ably/chat';
const chatClient: ChatClient; // existing ChatClient instance
const room = await chatClient.rooms.get('critical-updates');
// Handle discontinuities to ensure no messages are missed
const discontinuitySubscription = room.onDiscontinuity((reason) => {
console.warn('Discontinuity detected:', reason);
// Show warning to user
showDiscontinuityWarning('Connection interrupted - fetching missed messages...');
// You may also want to fetch missed messages to fill gaps during the discontinuity.
});
// Attach to the room to start receiving events
await room.attach();
// Clean up
discontinuitySubscription.off();
Registers a listener to be notified of room status changes.
Status changes indicate the room's connection lifecycle. Use this to monitor room health and handle connection issues over time.
Callback invoked when the room status changes
Subscription object with an unsubscribe method
import * as Ably from 'ably';
import { ChatClient, RoomStatus } from '@ably/chat';
const chatClient: ChatClient; // existing ChatClient instance
const room = await chatClient.rooms.get('support-chat');
// Monitor room status changes
const statusSubscription = room.onStatusChange((change) => {
console.log(`Room status: ${change.previous} -> ${change.current}`);
console.log(`Timestamp: ${change.timestamp.toISOString()}`);
// Handle different status transitions
switch (change.current) {
case RoomStatus.Attached:
console.log('Room is now connected');
enableChatUI();
showOnlineIndicator();
break;
case RoomStatus.Attaching:
console.log('Connecting to room...');
showConnectingSpinner();
break;
// Handle other cases as needed
}
});
// Clean up when done
statusSubscription.off();
Returns a copy of the options used to configure the room.
Provides access to all room configuration including presence, typing, reactions, and occupancy settings. The returned object is a deep copy to prevent external modifications to the room's configuration.
A deep copy of the room options
import { ChatClient } from '@ably/chat';
const chatClient = new ChatClient(realtime);
// Create room with specific options
const room = await chatClient.rooms.get('conference-hall', {
presence: {
enableEvents: true,
syncPresenceOnEntry: true
},
typing: {
heartbeatThrottleMs: 1500
},
occupancy: {
enableEvents: true
},
messages: {
rawMessageReactions: false
}
});
// Get room options to check configuration
const options = room.options();
console.log('Room configuration:');
console.log('Presence events:', options.presence?.enableEvents);
console.log('Typing throttle:', options.typing?.heartbeatThrottleMs);
console.log('Occupancy events:', options.occupancy?.enableEvents);
Represents a chat room.